Foreach使用相同的ID插入

时间:2016-02-11 08:13:35

标签: php mysql

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

示例:

tbl_products

+------------+--------------+-----------+-------+
| 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: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 |           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 |
+-----------+--------------+----------------+---------------+-----------------+

2 个答案:

答案 0 :(得分:0)

$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放入('')请检查

答案 1 :(得分:0)

根据您的评论,order_id会自动在表order中递增,这就是您无法使用相同ID的原因。更改表格并从order_id中删除自动增量。

ALTER TABLE orders CHANGE order_id order_id INT(10) UNSIGNED NOT NULL

每次插入值时都必须传递order_id

替代方式。

正如你所说

  

CUSTOMER 3从VENDOR 1购买产品A和B并购买产品D和E.   从VENDOR 2我想插入两个相同的order_id   表

然后你应该创建一个索引totalprice并添加这些产品的总数。然后用totalprice单次执行插入操作。