将多个数据插入一个表中

时间:2021-11-23 16:45:58

How to insert multiple data in the database? when I insert data that has multiple orders, only one item is inserted in the database. I need help putting this into a loop.

如何在数据库中插入多个数据?当我插入具有多个订单的数据时,在数据库中只插入一个项目。我需要帮助把它放到一个循环中。

Here's the code I'm currently using:

这是我目前使用的代码:

foreach ($_SESSION["cart_array"] as $each_items){
    $item_id = $each_items['item_id'];
    $quantity = $each_items['quantity'] ;
    $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'");
    while($row = mysql_fetch_array($sql)){
    $product_name = $row['name'];
    $price = $row['price'];
    $total_price = $price * $quantity;
    mysql_query("INSERT INTO customer_order(
    id,quantity,item_id,
    total_price,shipping_address,
    shipping_date,customer_id)
    VALUES ('','$quantity','$item_id','$total_price',
    '','',
    '$lastId')") or die (mysql_error());
    }
}

Here's what I tried but it's generating a syntax error:

这是我尝试过但它产生语法错误:

foreach ($_SESSION["cart_array"] as $each_items){
    $item_id = $each_items['item_id'];
    $item_id_count = count($item_id) ;
    $quantity = $each_items['quantity'] ;
    $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'");
    while($row = mysql_fetch_array($sql)){
       $product_name = $row['name'];
       $price = $row['price'];
       $total_price = $price * $quantity;
       foreach($i=0,$i < $item_id_count,$i++){
          mysql_query("INSERT INTO customer_order(
          id,quantity,item_id,
          total_price,shipping_address,
          shipping_date,customer_id)
          VALUES ('','$quantity','$item_id','$total_price',
          '','',
          '$lastId')") or die (mysql_error());
       }
    }
}

How can I correctly write the loop?

我怎样才能正确编写循环?

1 个解决方案

#1


0  

You have written foreach($i=0,$i < $item_id_count,$i++) where I think you meant

你写了foreach($ i = 0,$ i <$ item_id_count,$ i ++)我觉得你的意思

for ($i=0 ; $i < $item_id_count ; $i++ )

#1


0  

You have written foreach($i=0,$i < $item_id_count,$i++) where I think you meant

你写了foreach($ i = 0,$ i <$ item_id_count,$ i ++)我觉得你的意思

for ($i=0 ; $i < $item_id_count ; $i++ )