Decrement value in mysql but not negative
Add another condition to update only if the field
is greater 0
UPDATE table
SET field = field - 1
WHERE id = $number
and field > 0
You could prevent the new value to drop below zero by using GREATEST()
. If the value drops below zero, zero will always be greater than your calculated value, thus preventing any value below zero to be used.
UPDATE table
SET field = GREATEST(0, field - 1)
WHERE id = $number
And on a side note: Please don't use mysql_*
functions any more. They are deprecated and will eventually be removed from PHP. Use PDO or MySQLi instead.