The most important thing to pay attention to when inserting data into mysql is to prevent repeated adding of data. The following article mainly introduces to you the relevant information on how MYSQL prevents repeated adding when adding a shopping cart. The article uses examples The code is introduced in very detail. Friends who need it can refer to it. Let’s take a look together.
Preface
Recently due to work reasons, I was working on the APP shopping cart order payment. A bug was raised during the test. When the click to add to the shopping cart is relatively fast, the same product appears twice in the shopping cart.
Because when adding to the shopping cart, there are two steps. The first step is to first determine whether to add to the shopping cart. Check whether the products in the cart are already in the shopping cart. If they are, add one to the original quantity. If they are not there, plug in again.
Because the two steps are not atomic operations, they appear. There is not much to say about multi-threaded security issues. Come and follow the editor to see the detailed solution process. It will be of certain help to you.
MySQL insert has a more advanced operation
Sample code:
INSERT INTO t_xs_shopping_cart ( user_id, shop_id, commodity_id, quantity ) VALUES (71, 67, 140201057403511024, 1) ON DUPLICATE KEY UPDATE quantity = quantity + 1
When the unique key constraint takes effect, the update statement will be executed and the quantity will be increased by 1
Summary
The above is the detailed content of MYSQL implementation of code examples to prevent duplication of adding shopping carts. For more information, please follow other related articles on the PHP Chinese website!