简体   繁体   中英

PHP, SQL Check if the value is greater or less than the database value

I have this code

$str = $conn-prepare("UPDATE users SET ss = ss + $sss WHERE user=? and $sss > ss and $sss < ss");

But it does not works as expected. If the value is greater than the database value or if the value is less than the database value, it should do nothing, otherwise it should add the value with the current value. The ss means the database value and $sss means the value we want to add. So before adding the sss value to ss, it would check if greater or if less.

This should do what you want:

$str = $conn-prepare("UPDATE users SET ss = ss + $sss WHERE user=? AND ss <> $sss");

Note that query might be insecure. You should make sure $sss is a safe value (such as an integer), otherwise you might be risking an SQL injection attack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM