[英]How to add a id for many stock to be insert to database sql
I have a dynamic add form field function to add many stock in 1 order to database. 我有一个动态的添加表单字段功能,可以按1个顺序向数据库添加许多股票。
However I was unable to insert the same order_id for the order that contain many stock. 但是,我无法为包含很多库存的订单插入相同的order_id。 I need to use the order_id to print back the order detail 我需要使用order_id打印回订单详细信息
ordertable
--------------------------------------------------------
| order_id | order_item_id | item_name | item_quantity |
--------------------------------------------------------
stocktable
-----------------------------------------------------
| item_id | item_name | item_descript | item_quantity |
-----------------------------------------------------
<?php
require_once('../config.php');
$number = count($_POST["item_name"]);
$item_name=$_POST['item_name'];
$item_quantity=$_POST['item_quantity'];
$order_id = uniqid();
if($number > 0)
{
for($i=0; $i<$number; $i++)
{
if($item_name[$i]!="" && $item_quantity[$i]!="")
{
$sql = "INSERT INTO ordertable(order_id,item_name,item_quantity)
VALUES('$order_id','$item_name[$i]','$item_quantity[$i]')";
mysqli_query($conn, $sql);
}
}
}
$conn->close();
?>
Your schema needs some changes. 您的架构需要一些更改。 stocktable
contains a list of stock items. stocktable
包含库存项目列表。 ordertable
should contain a list of orders, and you are missing a orderdetailtable
which contains each of the stock items for a given order. ordertable
应该包含一个订单列表,而您缺少一个orderdetailtable
,其中包含给定订单的每个库存项目。
The problem is that in your schema, you have the order table, with the primary key of order_id
and you are expecting to put all the order line-items in with the same order_id
... This isn't going to work. 问题在于,在您的模式中,您具有订单表,该表具有order_id
的主键,并且您希望将所有订单行项目都放入相同的order_id
……这将行不通。
Create a new table to hold the order line-items, and have the order table with the order-specific information, such as customer, date, etc. The order detail table can be seen as a link table between orders and stock items. 创建一个新表来保存订单行项目,并使订单表包含特定于订单的信息,例如客户,日期等。订单明细表可以看作是订单和库存项目之间的链接表。
ordertable
------------------------------
| order_id | order_date | ...
------------------------------
orderdetailtable
----------------------------------------------------------
| order_detail_id | order_id | stock_id | order_quantity |
----------------------------------------------------------
stocktable
--------------------------------------------------------
| stock_id | item_name | item_descript | item_quantity |
--------------------------------------------------------
UPDATE The code above will need to change as well, to cater for the new database structure. 更新上面的代码也将需要更改,以适应新的数据库结构。
...
if($number > 0)
{
$sql = "INSERT INTO ordertable(order_id, ...) VALUES('$order_id', ...";
mysqli_query($conn, $sql);
for($i=0; $i<$number; $i++)
{
$order_detail_id = uniqid();
if($item_name[$i]!="" && $item_quantity[$i]!="")
{
$sql = "INSERT INTO orderdetailtable(order_detail_id,order_id,stock_id,item_quantity)
VALUES('$order_detail_id','$order_id','$item_id[$i]','$item_quantity[$i]')";
mysqli_query($conn, $sql);
}
}
...
The problem is that you are using uniqid()
, which is not appropriate for a table id for several reasons, but the relevant one to your problem is that it will return not an integer but an alphanumeric string like this: 问题是您使用的uniqid()
出于多种原因不适用于表id,但是与您的问题相关的一个问题是它将返回的不是整数,而是一个字母数字字符串,如下所示:
59f9a69367e7f
When you try to use that value as the order_id
MySQL try to convert it to number stopping at the first non numeric character, so you always get "59". 当您尝试将该值用作order_id
MySQL会尝试将其转换为以第一个非数字字符停止的数字,因此您始终会得到“ 59”。
The simplest methods for fixing this is define the order_id
column with AUTO_INCREMENT
, in this way MySQL will generate automatically an unique identity for new rows. 解决此问题的最简单方法是使用AUTO_INCREMENT
定义order_id
列,这样MySQL将自动为新行生成唯一标识。 If you need to get that id for later use you can get it using mysqli_insert_id()
. 如果您需要获取该ID以便以后使用,则可以使用mysqli_insert_id()
获得它。
Note that when inserting in tables with auto incremented id you skip the id column from the column list, in your case you will use: 请注意,当插入具有自动递增ID的表时,请从列列表中跳过ID列,在这种情况下,您将使用:
INSERT INTO ordertable (item_name,item_quantity)
As a final note, you code is vulnerable to SQL injection , you should use mysqli_prepare()
and mysqli_stmt_bind_param()
. 最后一点, 您的代码容易受到SQL注入的攻击 ,应使用mysqli_prepare()
和mysqli_stmt_bind_param()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.