0

Hi guys Good day, how to insert multiple data in the database? when i insert data that has multiple orders, only one item is inserted in the database. pls help me to put this on loop.

Heres the code i use:

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

                            }

            }

Heres what i did but it gives me syntax error, pls help me to do the proper looping for this. thanks

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

                }
            }

        }

Edited by diether.silverious

4
Contributors
5
Replies
37
Views
4 Years
Discussion Span
Last Post by Webville312
0

@pixelsoul . there is no error message ,it just happen that only one item is inserted in the table, although i choose multiple items.
and it gives me syntax error in this line
foreach($i=0,$i < $item_id_count,$i++)..

Edited by diether.silverious

1

the 2nd code snippet is wrong as first you are iterating for each items(although iterating is allowed only for arrays and objects) and then for particaular item ,you are inserting it n times .
The first code snippet is right.use that one instead of second

The correct syntax for foreach is

foreach (array_expression as $value)

foreach (array_expression as $key => $value)

It must be for loop not foreach

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

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.
http://php.net/manual/en/control-structures.foreach.php

Edited by IIM

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.