To prevent user's balance to ever go negative
(besides from the checks on the PHP side) I used the following BEFORE INSERT
Mysql Trigger:
BEGIN
UPDATE `table` SET `user_balance` = user_balance - NEW.amount WHERE `uid` = NEW.id;
-- Some other inserts etc...
END
I thought as this is a BEFORE INSERT
trigger, it would prevent insertion of any new invoice that might make the balance negative but it doesn't.
Even though the STRICT MODE is ON and the user_balance
column is UNSIGED DECIMAL
, the trigger doesn't stop the insertion and the field is simply turned into 0 if it's negative...
I tried running an UPDATE query manually, but it throws an error as expected, the trigger doesn't act the same.
Edit:
Does any one know why it works fine on a manual
UPDATE
, but not with the trigger?