您的 SQL 语法有错误;靠近 'SET `points` = `points` + ?'

不能说

我收到此错误,这似乎应该很容易找到,但是无论我如何查看代码或 Google 代码,都无法弄清楚。

这是代码

INSERT INTO `users` (`username`)
    VALUES (?)
        ON DUPLICATE KEY
            UPDATE `users` SET `points` = `points` + ?

这 ?后来使用准备好的语句给定值,但是为了测试,我用下面的实际值替换了它们

INSERT INTO `users` (`username`)
    VALUES (`existingusername`)
        ON DUPLICATE KEY
            UPDATE `users` SET `points` = `points` + 5

而且我也尝试nonextistingusername作为一个值。我尝试使用WHEREforUPDATE但相信它不需要,因为我正在使用ON DUPLICATE KEY UPDATE

我不知道我哪里出错了

编辑:我也试过在“ points+5”周围加括号

编辑:进一步的代码

foreach ($users as $user)
{
    $points_query = mysqli_prepare($db, 'INSERT INTO `users` (`username`)
        VALUES (?)
            ON DUPLICATE KEY
                UPDATE `users` SET (`points` = `points` + ?)');

    mysqli_stmt_bind_param($points_query, 'si', $user, $user_points);
    mysqli_stmt_execute($points_query);

    echo mysqli_error($db) . "\n";
}

和 SQL 错误

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 'SET (`points` = `points` + ?)' at line 4
迈克尔 - sqlbot

正确的语法不会再次使用表名或关键字SET

ON DUPLICATE KEY
   UPDATE `points` = `points` + ?

https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章