gpt4 book ai didi

php - Foreach 插入具有相同 ID 的内容

转载 作者:行者123 更新时间:2023-11-29 21:30:00 24 4
gpt4 key购买 nike

我想插入具有相同 ID 的数据。

示例:

tbl_产品

+------------+--------------+-----------+-------+
| product_id | product_name | vendor_id | price |
+------------+--------------+-----------+-------+
| P1 | A | 1 | 500 |
| P2 | B | 1 | 500 |
| P3 | C | 1 | 500 |
| P4 | D | 2 | 1000 |
| P5 | E | 2 | 1000 |
| P6 | F | 2 | 1000 |
+------------+--------------+-----------+-------+

示例:CUSTOMER 3VENDOR 1 购买产品 A 和 B,并从 VENDOR 2 购买产品 D 和 E 我想插入两个表之间具有相同的 order_id,如下所示:

TBL:订单

+----------+------------+
| order_id | totalprice |
+----------+------------+
| O1 | 1000 |
| O2 | 2000 |
+----------+------------+

TBL:订单详细信息

+-----------+--------------+----------------+---------------+-----------------+
| detail_id | get_order_id | get_product_id | get_vendor_id | get_customer_id |
+-----------+--------------+----------------+---------------+-----------------+
| 1 | O1 | P1 | 1 | 3 |
| 2 | O1 | P2 | 1 | 3 |
| 3 | O2 | P4 | 2 | 3 |
| 4 | O2 | P5 | 2 | 3 |
+-----------+--------------+----------------+---------------+-----------------+

我的代码:

$getcustomerid = $_SESSION['member_id']; // get customer id from member who's logged in
if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){

mysql_query("insert into orders (orders_id, totalprice) values(NULL,'$totalprice' )") or die (mysql_error());

$getorderid = mysql_insert_id();

foreach($_SESSION["products"] as $product){
$getproductid = $product["product_id"];
$getvendorid = $product["member_id"]; // get vendor id from database
mysql_query("insert into orders_detail (detail_id, get_orders_id, get_products_id, get_vendor_id, get_customer_id) values(null, '$getorderid', '$getproductid', '$getvendorid', '$getcustomerid')") or die (mysql_error());
}
header('location:check_out.php');
}

但是我的代码通过将一个字段插入到订单表中并使用最新的 ID 插入 get_order_id 给出了结果,如下所示:

TBL:订单

+----------+------------+
| order_id | totalprice |
+----------+------------+
| O1 | 1000 |
+----------+------------+

和TBL:orders_detail

+-----------+--------------+----------------+---------------+-----------------+
| detail_id | get_order_id | get_product_id | get_vendor_id | get_customer_id |
+-----------+--------------+----------------+---------------+-----------------+
| 1 | O1 | P1 | 1 | 3 |
| 2 | O1 | P2 | 1 | 3 |
| 3 | O1 | P4 | 2 | 3 |
| 4 | O1 | P5 | 2 | 3 |
+-----------+--------------+----------------+---------------+-----------------+

最佳答案

$getcustomerid = $_SESSION['member_id']; // get customer id from member who's logged in
if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){

mysql_query("insert into orders (orders_id, totalprice) values(NULL,'$totalprice' )") or die (mysql_error());

$getorderid = mysql_insert_id();

foreach($_SESSION["products"] as $product){
$getproductid = $product["product_id"];
$getvendorid = $product["member_id"]; // get vendor id from database
mysql_query("insert into orders_detail (detail_id, get_orders_id, get_products_id, get_vendor_id, get_customer_id) values(null,'$getorderid', '$getproductid', '$getvendorid', '$getcustomerid')") or die (mysql_error());
}
header('location:check_out.php');

}

您必须将所有值放入 (''),您忘记将 $getorderid 放入 (''),请检查

关于php - Foreach 插入具有相同 ID 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35333949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com