2017-05-08 44 views
-2

我得到這個錯誤,看起來應該很容易找到,但無論我看了多少代碼或谷歌它,我都弄不明白。您的SQL語法有錯誤;靠近'SET` points` =`points` +?'

下面是代碼

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作爲值。我試着用WHEREUPDATE但相信是不需要的,因爲我使用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 
+0

您可以添加PHP代碼和錯誤消息的問題? – chris85

+0

@ chris85我已更新它。謝謝 – cantsay

回答