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

An article to understand the use of proxies in JavaScript

Table of contents What is an agent Basic knowledg...

Node uses koa2 to implement a simple JWT authentication method

Introduction to JWT What is JWT The full name is ...

Oracle deployment tutorial in Linux environment

1. Environment and related software Virtual Machi...

Detailed explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

A detailed introduction to JavaScript execution mechanism

Table of contents 1. The concept of process and t...

Detailed explanation of mysql user variables and set statement examples

Table of contents 1 Introduction to user variable...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...

Database query optimization: subquery optimization

1. Case Take all employees who are not the head o...

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...

jQuery clicks on the love effect

This article shares the specific code of jQuery&#...

vue cli3 implements the steps of packaging by environment

The vue project built with cli3 is known as a zer...

Introduction to common commands and shortcut keys in Linux

Table of contents 1 System Introduction 2 System ...