找到你要的答案

Q:Unable to UPDATE row, only INSERT to my MySQL table. Syntax Error “WHERE ID =0”

Q:无法更新的行,只有插入我的MySQL表。语法错误“id = 0”

I'm having a few issues as I'm new to MySQL and a PHP novice. On submissions of my form if I use the INSERT command it works fine. If I change the command to UPDATE, it can't find the 'ID' to update. I've replaced the WHERE ID with the exact row ID but comes back with sytax error.

Heres my code to add:

function create()
{
    $sql = sprintf(
        "INSERT minty_config 
        (name, value) 
        VALUES
        ('%s','%s')",
        $this->db->clean($this->name),
        $this->db->clean($this->value)
    );

    $this->db->query($sql);
}

Here's my code to update:

function update()
{
    $sql = sprintf(
        "UPDATE minty_config  SET
        name='%s', 
        value='%s', 
        WHERE ID=%d",
        $this->db->clean($this->name),
        $this->db->clean($this->value),
        $this->ID
    );

    $this->db->query($sql);
}

I'm guessing I need to change the following php so I can UPDATE instead of INSERT but everytime I try to make changes I just get a blank screen so something is wrong. This is the code:

    session_start();
    // Connection to server established here//
    include('../config.php');
    // Authentication of login here//
    if (!$user->authenticated)
    {
        header('Location: login.php');
        die();
    }
    //'text' is class where I have list of functions: get, set, update, create, etc. //
    if (isset($post->form_action))
    {
        $a = new text(false, $db);
        $a->name = $post->name;
        $a->value = $post->value;
    //this function works if use 'create' but when changed to 'update I get a Syntax error //   
        if (!$err)
        {
            $a->update();
            $succ = "Success!";
        }
    }

I've tried something like this to fix this WHERE ID syntax error but it returns a blank screen and I can't even see the form:

    $id = (isset($get->id) && is_numeric($get->id)) ? $get->id : ((isset($post->id) && is_numeric($post->id)) ? $post->id : false);
    if (!$id) die();

Any help would be appreciated.

Thanks a lot!

我有几个问题我新的MySQL和PHP新手。关于我的表格提交,如果我使用插入命令,它工作罚款。如果我更改命令更新,它找不到“ID”来更新。我用精确的行ID取代,但在错误ID回来。

这是我的代码添加:

function create()
{
    $sql = sprintf(
        "INSERT minty_config 
        (name, value) 
        VALUES
        ('%s','%s')",
        $this->db->clean($this->name),
        $this->db->clean($this->value)
    );

    $this->db->query($sql);
}

这是我的更新代码:

function update()
{
    $sql = sprintf(
        "UPDATE minty_config  SET
        name='%s', 
        value='%s', 
        WHERE ID=%d",
        $this->db->clean($this->name),
        $this->db->clean($this->value),
        $this->ID
    );

    $this->db->query($sql);
}

我想我需要改变以下PHP所以我可以更新插入但每次我试图做出改变,我刚刚得到一个空白的屏幕,什么是错的。这是代码:

    session_start();
    // Connection to server established here//
    include('../config.php');
    // Authentication of login here//
    if (!$user->authenticated)
    {
        header('Location: login.php');
        die();
    }
    //'text' is class where I have list of functions: get, set, update, create, etc. //
    if (isset($post->form_action))
    {
        $a = new text(false, $db);
        $a->name = $post->name;
        $a->value = $post->value;
    //this function works if use 'create' but when changed to 'update I get a Syntax error //   
        if (!$err)
        {
            $a->update();
            $succ = "Success!";
        }
    }

我已经尝试了这样的东西来修复这个ID语法错误,但它返回一个空白的屏幕,我什至不能看到的形式:

    $id = (isset($get->id) && is_numeric($get->id)) ? $get->id : ((isset($post->id) && is_numeric($post->id)) ? $post->id : false);
    if (!$id) die();

任何帮助将不胜感激。

谢谢!

answer1: 回答1:

Your update query has an extra , comma before where

"UPDATE minty_config  SET name='%s', value='%s', WHERE ID=%d"
                                              ^^^

It should be

"UPDATE minty_config  SET name='%s', value='%s' WHERE ID=%d"
                                              ^^

I've just removed that extra comma. This'll work for you.

您的更新查询有一个额外的,逗号之前

"UPDATE minty_config  SET name='%s', value='%s', WHERE ID=%d"
                                              ^^^

它应该是

"UPDATE minty_config  SET name='%s', value='%s' WHERE ID=%d"
                                              ^^

我刚刚去掉了多余的逗号。这将为你工作。

answer2: 回答2:

Sounds like your code is throwing an error somewhere. Set error_reporting(E_ALL) and see if anything comes up.

echo your query before you execute it and run it on the SQL server, does it work?

If it works, step through your code using debugger (or just use echo/print_r statements as breakpoints).

You said you were getting an error, what is it?

Post any info you find in your question and I can probably help more if you still need it.

听起来你的代码在某处丢了一个错误。集error_reporting(e_all)看看发生了什么事。

重复你的查询在执行它的SQL服务器上运行它,可以吗?

如果它的工作原理,使用调试器通过代码的步骤(或只是使用回声/ print_r语句断点)。

你说你有一个错误,是什么?

张贴任何信息,你发现你的问题,我可以帮助更多,如果你仍然需要它。

php  mysql  syntax-error