Home > Database > Mysql Tutorial > body text

MYSQL implementation of code examples to prevent duplication of adding shopping carts

黄舟
Release: 2017-09-21 10:53:36
Original
1736 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!