简体   繁体   English

如何在php&mysql中与并发用户生成下一个增量号?

[英]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. 我有一个系统,用户可以订购多个项目,假设用户将3个项目添加到购物车并提交订单,然后每个项目都插入到订单表中,但所有这些项目的订单号都相同。 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. 我正在从最后插入的订单号生成此order_no,因为我使用我的php脚本生成了订单号。 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. 您可以在Php中使用会话。 In session for each user will have order_id. 在会话中,每个用户将具有order_id。 You can also use cookies . 您也可以使用cookie You can easily maintain session variable for a user. 您可以轻松维护用户的会话变量。 This session variable contain the order_id value. 该会话变量包含order_id值。 what is latest order_id, store to database. 什么是最新的order_id,存储到数据库。 when new user come then read that order id from database and increment it then assign to user. 当新用户到来时,请从数据库中读取该订单ID并对其递增,然后将其分配给用户。 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. 但是在Internet上存在一个问题:Web服务器不知道您是谁或做什么,因为HTTP地址不保持状态。

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 更多@ w3schoolPHP 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. 数据库在orders表中插入一行,该行的内容类似于order_id int auto_increment primary key保证与下一个家伙不同。

Under this order_id you hang all your item/product purchases in something like an orderlines table (the order details). 在这个order_id下,您将所有购买的商品/产品挂在订单行表(订单明细)之类的地方。

There is zero chance of it screwing up with duplicates at the order_id level. 在order_id级别将其与重复项搞混的可能性为零。

Now, if you have to have some derived or otherwise custom other column with a WKO or some other prefix, that is possible too. 现在,如果您必须使用WKO或其他前缀添加一些派生或自定义的其他列,那也是可能的。 But the main thing is that the order_id is secured from collision with others. 但是最主要的是,order_id可以防止与其他对象冲突。

Read this for mysqli , read this for PDO . 请阅读有关mysqli的内容和有关PDO的内容

Mysql Using AUTO_INCREMENT MySQL 使用AUTO_INCREMENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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