mysql - 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
(当然,我不知道用户是如何识别的。)
?
用于传入的参数。您应该使用参数,而不是将值填充到字符串中。
推荐阅读
- google-cloud-platform - GCE:你如何让负载均衡器拒绝过多的流量?
- c# - 如何在单元测试中使用 NET.Core DI 服务?
- rx-java - 重用部分 RxJava 流
- node.js - 如何在 pug 模板中运行 moment.js?
- python - 作为 collections.Counter 的结果的字典切片
- javascript - 数据在 ajax 调用中作为空 json 数组对象传递
- html - 内部 div 如何占据视口高度的其余部分
- python - Python中DND字符的滚动统计
- reactjs - 为什么 React 不包含 shadow DOM?它不会比在 JS 解决方案中使用 CSS 更轻松吗?
- javascript - 如何使用 CSS 设置 mCustomScrollbar 的宽度