首页 > 解决方案 > Mysql更新用户余额SQL语法错误

问题描述

我想更新用户余额,但我不知道该怎么做。使用我的代码它不起作用:

 $sql = "UPDATE apiusers SET balance = balance - (product_price) WHERE username = (username);";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

表 apiuser:

+-------------+
| Field       | 
+-------------+
| id          | 
| user        |
| pass        | 
| balance     |
| lastip      | 
| lastlogind  |
| email       |
| verification|
+-------------+

标签: mysqlsql

解决方案


我会期待这样的事情:

update apiusers
    set balance = balance - ?   -- how much to change the balance
    where userid = ?;           -- which user to change it for

(当然,我不知道用户是如何识别的。)

?用于传入的参数。您应该使用参数,而不是将值填充到字符串中。


推荐阅读