【问题标题】:Inserting multiple data into one table将多条数据插入一张表
【发布时间】:2013-03-17 17:53:39
【问题描述】:

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

这是我目前使用的代码:

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

如何正确编写循环?

【问题讨论】:

标签: php mysql loops for-loop


【解决方案1】:

你写了foreach($i=0,$i &lt; $item_id_count,$i++) 我认为你的意思

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

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    相关资源
    最近更新 更多