为什么即使单元格更新正确,我也会收到 MySQL 语法错误?

Why do I get a MySQL syntax error even though the cell updates correctly?

抛出错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

相关代码:

$update = mysqli_real_escape_string($connection, $update);
// $update = '10'; $id = '4'
$sql = "UPDATE tasks SET `shortDesc` = '".$update."' WHERE `id`=".$id;
if (mysqli_query($connection, $sql) === TRUE) {
    echo 1;
} else {
    echo mysqli_error($connection);
}

我无法在论坛上找到任何类似的有帮助的错误。 为什么它工作但抛出错误?

您发布的代码应该不会导致错误,因为参数 $id 永远不会为空。实际上查看您的错误消息和您的查询,这可能是唯一产生的错误,因为如果两个变量都是空的,您将有:

UPDATE tasks SET `shortDesc` = '' WHERE `id`=

这会失败,因为 id 没有值。由于 MySQL 对引号中的数字没有问题,这应该消除错误消息:

$sql = "UPDATE tasks SET `shortDesc` = '".$update."' WHERE `id`='".$id."'";

您可以优化它以获得更好的阅读效果:

$sql = "UPDATE tasks SET `shortDesc` = '$update' WHERE `id`='$id'";

现在如果变量为空,查询仍然不会无效。

由于这是或曾经是您代码中的唯一错误来源,我认为您的列表中缺少某些内容,或者您​​的代码被调用两次但缺少参数?

为了质量保证,您应该在使用之前检查 $update 和 $id 的有效性。至少确保它们不是空的,这样你就可以省去电话了。