我正在实施服装购物网站,其中购物车商品在特定客户的数据库中插入。我有两个表order和ordered_products。在订单表中,order_id是主键,此表包含客户信息。 Ordered_products表包含购物车中的产品。我试图从订单表中使用最后一个插入ID函数获取订单ID,但仅针对第一个项目,它从订单中获取order_id,而对于所有其他项目,它从ordered_products表中获取第一个项目的ID。 这是我的PHP代码,
In [41]: pd.to_datetime(df["MESS_DATUM"].astype(str).str.split('.').str[0],
format='%Y%m%d%H')
Out[41]:
0 2017-08-07 19:00:00
1 2017-08-07 20:00:00
2 2017-08-07 21:00:00
3 2017-08-07 22:00:00
4 2017-08-07 23:00:00
Name: MESS_DATUM, dtype: datetime64[ns]
答案 0 :(得分:0)
echo mysqli_insert_id($con)
$qry="INSERT INTO orders ( customer_id , customer_name, customer_email) VALUES ((SELECT user_id from users where email='$email'), (SELECT name from users where email='$email'), '$email' )" ;
$result=mysqli_query($con,$qry) or die(mysqli_error($con));
if($result)
{
echo mysqli_insert_id($con);
...
}
答案 1 :(得分:0)
您可以使用此插件替换插入查询。所以order_id对所有人来说都是一样的。
$order_id = mysqli_insert_id($con);
$qry="INSERT INTO ordered_product ( order_id , product_code, product_description, Product_gender, Product_quantity, Product_price) VALUES ('$order_id', '$product_code', '$description', '$gender', '$quantity', '$price' ) ";
$result=mysqli_query($con,$qry) ;
答案 2 :(得分:0)
此问题是
执行此查询后
$ qry =" INSERT INTO订单(customer_id,customer_name,customer_email)VALUES((来自用户的user_id,其中email =' $ email'),(来自用户的选择名称,其中email =& #39; $ email'),' $ email')" ;
SELECT LAST_INSERT_ID()将是order_id
之后执行
$ qry =" INSERT INTO ordered_product(order_id,product_code,product_description,Product_gender,Product_quantity,Product_price)VALUES((SELECT LAST_INSERT_ID()),' $ product_code',' $说明',' $ gender',' $ quantity',' $ price')";
然后LAST_INSERT_ID()将使用ordered_product表主键值进行更新。因此,你得到了问题。
尝试在临时变量中保存LAST_INSERT_ID()并进一步使用。
用以下代码替换您的代码,
<?php
if (isset($_POST['placeorder']))
{
$email=$_SESSION["email"];
$con=mysqli_connect("localhost", "root", "");
mysqli_select_db($con,"login");
$qry="INSERT INTO orders ( customer_id , customer_name, customer_email) VALUES ((SELECT user_id from users where email='$email'), (SELECT name from users where email='$email'), '$email' )" ;
$result=mysqli_query($con,$qry) or die(mysqli_error($con));
$order_id = mysql_insert_id();
if($result)
{
for ($i=0; $i<count($_POST['ID']); $i++){
$product_code = $_POST['ID'][$i];
$gender = $_POST['gender'][$i];
$price = $_POST['grandtotal'][$i];
$quantity = $_POST['qty'][$i];
$description = $_POST['description'][$i];
$qry="INSERT INTO ordered_product ( order_id , product_code, product_description, Product_gender, Product_quantity, Product_price) VALUES ($order_id, '$product_code', '$description', '$gender', '$quantity', '$price' ) ";
$result=mysqli_query($con,$qry) ;
if($result)
{
echo '<script>alert("Your order has been placed")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
die("Error While Adding Stock ! Please Try Again .");
}
}}}
?>