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

 






 

Link to comment
Share on other sites

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

Edited by subhomoy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.