简体   繁体   中英

How to generate next increment number with concurrent user in php&mysql?

I have a system where users can order multiple items, lets say a user added 3 items to the cart and submit the order then every item gets inserted to the order table but order number is same for all these items. ex below.

order_id product_id    order_no 
10895    ACP1001       WKO00000003659 
10894    ACP1000       WKO00000003659 
10893    ACP1001       WKO00000003658 
10892    ACP1000       WKO00000003658 

I am generating this order_no from the last inserted order number, for that I use my php script to generate the order number. Everything works perfectly except when there are simultaneous orders. If there are simultaneous orders from different users then the same order number is being assigned to all users. How can I overcome this situation?

You can use session in Php for this. In session for each user will have order_id. You can also use cookies . You can easily maintain session variable for a user. This session variable contain the order_id value. what is latest order_id, store to database. when new user come then read that order id from database and increment it then assign to user. After update to database.

what is session-

When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.

Session variables solve this problem by storing user information to be used across multiple pages (eg username, favorite color, etc). By default, session variables last until the user closes the browser.

So; Session variables hold information about one single user, and are available to all pages in one application.

I am settng an example only. It may be differ for your case.

 mysql > desc order_id_generate;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_SESSION['order_id']) && !empty($_SESSION['order_id'])) {
   echo "Already ordered set and not empty ! ".$_SESSION[`order_id`];
}
else {
   $query = "INSERT INTO order_id_generate VALUES ()";
   $mysqli->query($query);
   $temp=$mysqli->insert_id;
   // set the new order
   $_SESSION['order_id']=$temp;
   echo "Ordered set and not empty ! ".$_SESSION[`order_id`];
}

$conn->close();
?>
</body>
</html>

more @ w3school and php mannual

A user, any user, creates an order. The db inserts a row in an orders table that has something like order_id int auto_increment primary key guaranteed to be different from the next guy.

Under this order_id you hang all your item/product purchases in something like an orderlines table (the order details).

There is zero chance of it screwing up with duplicates at the order_id level.

Now, if you have to have some derived or otherwise custom other column with a WKO or some other prefix, that is possible too. But the main thing is that the order_id is secured from collision with others.

Read this for mysqli , read this for PDO .

Mysql Using AUTO_INCREMENT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM