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|
+-------------+

我希望这样:

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

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

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

你忘了你的平等

$sql = "UPDATE apiusers 
SET balance = (balance - product_price)
WHERE user = "".$username."";";

\"用户名",因为 " 将关闭$sql的字符串,并且使用 \" 可以防止这种情况。

您需要一个"位置",以便您可以使用 ID xy 更新列。

最新更新