0

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

这是我目前正在使用的代码:

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());
    }
}

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

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());
       }
    }
}

如何正确编写循环?

4

1 回答 1

0

你写foreach($i=0,$i < $item_id_count,$i++)的地方我认为你的意思

for ($i=0 ; $i < $item_id_count ; $i++ )
于 2013-03-17T18:06:12.293 回答