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
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).
ALTER TABLE posts MODIFY votes BIGINT SIGNED DEFAULT 0;
Something like this should help.
first run query to make votes a signed column as suggested by Leon
ALTER TABLE t1 MODIFY votes BIGINT SIGNED;
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