Help with Form outcome

I have a Form to submit that works successfully, except one part.

//Wallet check 
if (empty($error) && $_POST['set_p_v'] == 0 && $wallet <1){
$error = "not enough money";

actually this code successfully displays the correct error message “not enough money” when 0 is entered in the Form field and the ‘wallet’ amount is 0, but upon testing, when the ‘wallet’ amount is greater than 0, and the Form field is 0, and I try to submit the Form, I still see that same error.

Ultimately, success would be if upon entering 0 in the Form field, with any amount in the ‘wallet’ equal to or greater than 1, the Form submits/proceeds/succeeds.
I don’t know if it’s imprtant to know that in the db Table field ‘wallet’, in phpmyadmin, the Type is Float.
I look forward to any suggestions/solution on this…


What you see as an incorrect outcome from a form submission, is just the tail-end snippet of the problem. If this code is attempting to get the current wallet amount, subtracting the purchase amount, and testing the result, it will fail when there are multiple concurrent instances of your script running, since they will all determine that there is enough money, and all the purchase, resulting in spending more money than is available.

To do this correctly, the test for enough money and inserting a row of data accounting for the purchase, must occur in a single (atomic) database operation. The way to do this is to build an INSERT into the accounting table… SELECT the literal values to insert here… WHERE the money calculation is TRUE. You would then test if the insert query executed without any error and actually inserted a row. If it did, then there was enough money for the purchase. If it did not, then you would setup the - “not enough money” message.

Sponsor our Newsletter | Privacy Policy | Terms of Service