gpt4 book ai didi

php - 无法更新行,只能插入到我的 MySQL 表中。语法错误 "WHERE ID =0"

转载 作者:行者123 更新时间:2023-11-29 02:56:45 25 4
gpt4 key购买 nike

由于我是 MySQL 新手和 PHP 新手,我遇到了一些问题。如果我使用 INSERT 命令提交我的表单,它工作正常。如果我将命令更改为 UPDATE,它找不到要更新的“ID”。我已将 WHERE 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!";
}
}

我试过这样的方法来修复这个 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();

如有任何帮助,我们将不胜感激。

非常感谢!

最佳答案

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

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

应该是

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

我刚刚删除了多余的逗号。这对你有用。

关于php - 无法更新行,只能插入到我的 MySQL 表中。语法错误 "WHERE ID =0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com