Dear sir
any one help me on this code . inserting mutilple data in mysql table but i receive only 1 data row

include("admin/db_connect/data_connect.php");
session_start();
//session_destroy();
if(isset($_POST['complete_order']))

{
    $_SESSION['cart'][]=array('quantity' => $_POST['quantity'],'fast_image' => $_POST['fast_image'],'menu_name' => $_POST['menu_name'],'price' => $_POST['price']);
    foreach($_SESSION['cart']as $keys => $val)
    {
          $implode=implode(",",$val);
        }
    $insert_data="INSERT INTO `customer_order` (`quantity`, `fast_image`, `menu_name`, `price`) VALUES ('$val[0]', '$val[1]', '$val[2]', '$val[3]')";
        $mysql_query=mysql_query($insert_data);     
}

Recommended Answers

As rproffitt states, your MySQL query on lines 12 and 13 are happening outside of the loop, and just for the latest value of $val after the loop finishes executing (what $val last was in the last iteration of the loop).

What I think you're trying to accomplish is insert …

Jump to Post

All 2 Replies

Look at lines 8 to 11. That's your foreach section. Your insert is outside this loop so it looks from here it would have a single insert.

As rproffitt states, your MySQL query on lines 12 and 13 are happening outside of the loop, and just for the latest value of $val after the loop finishes executing (what $val last was in the last iteration of the loop).

What I think you're trying to accomplish is insert multiple rows at once. You can do that with a MySQL query like this to insert three rows simultaneously:

INSERT INTO customer_order (quantity, fast_image, menu_name, price)
VALUES (a, b, c, d), (e, f, g, h), (i, j, k, l)

This code is thoroughly untested, but try something roughly along these lines:

$insert_data = array();

foreach($_SESSION['cart'] as $keys => $val)
{
    // Add to array
    $insert_data[] = "('$val[0]', '$val[1]', '$val[2]', '$val[3]')";
}

$implode=implode(",",$insert_data);

$query="INSERT INTO `customer_order` (`quantity`, `fast_image`, `menu_name`, `price`) VALUES $implode";

$mysql_query=mysql_query($query);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.