无法UPDATE行,只能INSERT到我的MySQL表。语法错误“WHERE ID = 0”

时间:2021-01-05 23:01:45

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.

我有一些问题,因为我是MySQL新手和PHP新手。如果我使用INSERT命令提交我的表单,它工作正常。如果我将命令更改为UPDATE,则无法找到要更新的“ID”。我已经用确切的行ID替换了WHERE ID,但是返回了sytax错误。

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:

我猜我需要更改以下的php所以我可以更新而不是INSERT但每次我尝试进行更改时我只是得到一个空白的屏幕,所以有些错误。这是代码:

    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:

我尝试过这样的东西来修复这个WHERE ID语法错误,但它返回一个空白屏幕,我甚至看不到表单:

    $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!

非常感谢!

2 个解决方案

#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.

我刚刚删除了那个额外的逗号。这对你有用。

#2


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

听起来你的代码在某个地方抛出错误。设置error_reporting(E_ALL)并查看是否有任何问题。

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

在执行它之前回显您的查询并在SQL服务器上运行它,它是否有效?

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

如果它工作,使用调试器逐步执行代码(或者只使用echo / print_r语句作为断点)。

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.

发布您在问题中找到的任何信息,如果您仍然需要,我可能会提供更多帮助。

#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.

我刚刚删除了那个额外的逗号。这对你有用。

#2


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

听起来你的代码在某个地方抛出错误。设置error_reporting(E_ALL)并查看是否有任何问题。

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

在执行它之前回显您的查询并在SQL服务器上运行它,它是否有效?

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

如果它工作,使用调试器逐步执行代码(或者只使用echo / print_r语句作为断点)。

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.

发布您在问题中找到的任何信息,如果您仍然需要,我可能会提供更多帮助。