[英]Increment value in MySQL update query
I have made this code for giving out +1 point, but it doesn't work properly.我已经制作了这个代码来给出+1分,但它不能正常工作。
mysql_query("
UPDATE member_profile
SET points= ' ".$points." ' + 1
WHERE user_id = '".$userid."'
");
The $points
variable is the user's points right now. $points
变量是用户现在的积分。 I want it to add one to it.我希望它添加一个。 So example if he had like 5 points, it should be 5+1 = 6, but it doesn't, it just changes to 1.
例如,如果他有 5 分,应该是 5+1 = 6,但事实并非如此,它只是变为 1。
What have I done wrong?我做错了什么?
Simply increment the value that already exists in the database只需增加数据库中已经存在的值
$sql = "UPDATE member_profile SET points = points + 1 WHERE user_id = ?";
$db->prepare($sql)->execute([$userid]);
This code would work for both PDO and mysqli in the modern PHP versions此代码适用于现代 PHP 版本中的 PDO 和 mysqli
您可以执行此操作而无需查询实际的点数,因此它会在脚本执行期间为您节省一些时间和资源。
mysql_query("UPDATE `member_profile` SET `points`= `points` + 1 WHERE `user_id` = '".intval($userid)."'");
"UPDATE member_profile SET points = points + 1 WHERE user_id = '".intval($userid)."'"
Hope I'm not going offtopic on my first post, but I'd like to expand a little on the casting of integer to string as some respondents appear to have it wrong.希望我的第一篇文章不会偏离主题,但我想稍微扩展一下整数到字符串的转换,因为一些受访者似乎错了。
Because the expression in this query uses an arithmetic operator (the plus symbol +), MySQL will convert any strings in the expression to numbers.因为此查询中的表达式使用算术运算符(加号 +),所以 MySQL 会将表达式中的任何字符串转换为数字。
To demonstrate, the following will produce the result 6:为了演示,以下将产生结果 6:
SELECT ' 05.05 '+'.95';
String concatenation in MySQL requires the CONCAT() function so there is no ambiguity here and MySQL converts the strings to floats and adds them together. MySQL 中的字符串连接需要 CONCAT() 函数,因此这里没有歧义,MySQL 将字符串转换为浮点数并将它们相加。
I actually think the reason the initial query wasn't working is most likely because the $points variable was not in fact set to the user's current points.我实际上认为初始查询不起作用的原因很可能是因为 $points 变量实际上并未设置为用户的当前点。 It was either set to zero, or was unset: MySQL will cast an empty string to zero.
它要么设置为零,要么未设置:MySQL 会将空字符串强制转换为零。 For illustration, the following will return 0:
为了说明,以下将返回 0:
SELECT ABS('');
Like I said, I hope I'm not being too off-topic.就像我说的,我希望我不会太离题。 I agree that Daan and Tomas have the best solutions for this particular problem.
我同意 Daan 和 Tomas 对这个特定问题有最好的解决方案。
此外,要“增加”字符串,更新时,使用CONCAT
update dbo.test set foo=CONCAT(foo, 'bar') where 1=1
Who needs to update string and numbers谁需要更新字符串和数字
SET @a = 0;
UPDATE obj_disposition SET CODE = CONCAT('CD_', @a:=@a+1);
The accepted answer is good but not working with null values try this接受的答案很好,但不能使用空值试试这个
mysql_query("
UPDATE member_profile
SET points = IFNULL(points, 0) + 1
WHERE user_id = '".$userid."'
");
You should use PDO to prevent SQL injection risk.您应该使用 PDO 来防止 SQL 注入风险。
You can connect to DB like this :您可以像这样连接到数据库:
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=xxxx;dbname=xxxx;charset=utf8mb4', 'user', 'password', $pdo_options);
No need to query DB to get the number of points.无需查询数据库即可获得点数。 You can increment directly in the update query (
points = points + 1
).您可以直接在更新查询中递增(
points = points + 1
)。
(note : Also, it's not a good idea to increment the value with PHP because you need to select first the data and the value can changed if other users are updated it.) (注意:另外,使用 PHP 增加值也不是一个好主意,因为您需要先选择数据,如果其他用户更新它,值可能会改变。)
$req = $bdd->prepare('UPDATE member_profile SET
points = points + 1
WHERE user_id = :user_id');
$req->execute(array(
'user_id' => $userid
));
Remove the '
around the point
:删除
point
周围的'
:
mysql_query("UPDATE member_profile SET points=".$points."+1 WHERE user_id = '".$userid."'");
You are "casting" an integer value to string in your original query...您正在原始查询中将整数值“转换”为字符串...
为什么不让 PHP 完成这项工作?
"UPDATE member_profile SET points= ' ". ($points+1) ." ' WHERE user_id = '".$userid."'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.