i am trying to create a ecommerce website for my school project,but i am confused about when to update the quantities in the database.When i am going to add the product to cart or when i complete the order?If i decrease the quantity when i add the product in the cart and the customer doesn’t complete the order then i must increase the quantity again.So the only way is to update the quantity when a customer completes the order,and if 2 customers go to the final page together then to put an if statement to check the quantity in order to finalize the order,and if one of the 2 customers took the last product then the other will get a message like”We are verry sorry,the last product sold 2 minutes before”.
Is this the right way of thinking?Any other idea?(Sorry for my english.)
i am trying to create a ecommerce website for my school project,but i am
Share
This is whats called a race condition.
In a situation like this you will need to update the stock for the first customer to complete their transaction in a transaction and notify the other customer that the stock is gone , or use your first idea and decrement the stock but if the customer cancels reincrement. Either should work with planning.