Reputation: 15037
UPDATE posts set votes=votes+1 where id='$id'
The above works when the value is zero, but when i Subtract (current value of 0):
UPDATE posts set votes=votes-1 where id='$id'
It's having an error. Why is that so?
datatype is int
Upvotes: 1
Views: 581
Reputation: 882806
You no doubt have one of the unsigned integral types. These all have a minimum value of zero, so you cannot make these types negative.
Either change your code so this doesn't happen or change the datatype to a signed one.
The former can be done with something like:
update posts set votes = votes - 1 where id = '$id' and votes > 0
The latter with something like:
alter table post modify votes int signed
Upvotes: 1
Reputation: 7116
first run query to make votes a signed column as suggested by Leon
ALTER TABLE t1 MODIFY votes BIGINT SIGNED;
Upvotes: 1
Reputation: 58454
ALTER TABLE posts MODIFY votes BIGINT SIGNED DEFAULT 0;
Something like this should help.
Upvotes: 3
Reputation: 4894
Make the votes column signed (so untick the unsigned checkbox).
Unsigned integer columns (which is default in most tools) can only contain positive integers. Signed integers have an extra bit for the - (so they support negative values).
Upvotes: 2