Jump to content

How to insert multiple data in the table


Diether

Recommended Posts

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

 






 

you have left two blank spaces (i.e shipping_date & customer_id).. Check ur SQL query

 

 VALUES ('','$quantity','$item_id','$total_price',
'','',

either fill it up with the details or do this one to add more specifically

 

 mysql_query("INSERT INTO customer_order(id,quantity,item_id,total_price,customer_id)VALUES('','$quantity','$item_id','$total_price',
'$lastId')") or die (mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.