I’m making a Billing system with some friends, it works this way:
The customers make calls.
The customers hangup the call.
The price of the call is calculated.
The price of the call is reduced from the customer’s credit.
We decided to make the following:
Get the user’s balance and store it in a variable, $balance, after do a $balance = $balance – $callprice, and finally update the database.
The problem is that the customer can make simultaneous calls, and if two calls finish at the same time, and one of them gets the value on the database before the other script had updated the new balance… one of the calls will be lost. I’m using php.
Any idea how can I do it?
Thanks, and sorry, I have a poor English…
The problem is it looks like you’re trying to use two SQL statements to update the user’s balance: One to
SELECTthe user’s balance, then another toUPDATEthe user’s balance after the balance is subtracted using PHP.You could do it all in one operation and eliminate the possibility of race-conditions: