MYSQL implements sample code to prevent duplicate addition when adding shopping cart

MYSQL implements sample code to prevent duplicate addition when adding shopping cart

Preface

Recently, due to work reasons, I was working on the APP shopping cart order payment. A bug was found during testing. When I clicked Add to Cart quickly, the same product appeared twice in the shopping cart.

Because when adding to the shopping cart, there are two steps. The first step is to determine whether the product to be added to the shopping cart is already in the shopping cart. If it is, add one to the original quantity. If not, add it again.

Because the two steps are not atomic operations, there is a multi-threaded security issue. I won’t say much below. Let’s follow the editor to look at the detailed solution process, which will be of some 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 to increase the quantity by 1

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Several ways to add timestamps in MySQL tables
  • How to add indexes to MySQL
  • How to create a stored procedure in MySQL and add records in a loop
  • Summary of MySQL's commonly used SQL statements for creating tables, adding fields, modifying fields, and adding indexes
  • How to add fields and comments to a table in sql

<<:  What is this in JavaScript point by point series

>>:  Detailed explanation of how CocosCreator system events are generated and triggered

Recommend

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

How many times will multiple setStates in React be called?

Table of contents 1. Two setState, how many times...

The easiest way to reset mysql root password

My mysql version is MYSQL V5.7.9, please use the ...

Commonly used English fonts for web page creation

Arial Arial is a sans-serif TrueType font distribu...

How to install ionCube extension using pagoda

1. First install the pagoda Installation requirem...

Solve the problem of inconsistency between mysql time and system time in docker

Recently, when I installed MySQL in Docker, I fou...

MySQL controls the number of attempts to enter incorrect passwords

1. How to monitor MySQL deadlocks in production e...

Table of CSS Bugs Caused by hasLayout

IE has had problems for a long time. When everyone...

Ant designing vue table to achieve a complete example of scalable columns

Perfect solution to the scalable column problem o...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

Solution to PHP not being able to be parsed after nginx installation is complete

Table of contents Method 1 Method 2 After install...