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


    $_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)
    $insert_data="INSERT INTO `customer_order` (`quantity`, `fast_image`, `menu_name`, `price`) VALUES ('$val[0]', '$val[1]', '$val[2]', '$val[3]')";

Recommended Answers

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]')";


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

Be a part of the DaniWeb community

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