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

Dani AI

Generated

The thread from @imtiaz1 shows a session-backed cart being built and then written to the database; replies from and point to why the result is not what was expected. Recommended improvements that add correctness and safety: stop using the old mysql* API, inspect the runtime session structure before inserting, validate and cast POST values, and use prepared statements inside a transaction so all cart rows are inserted reliably or rolled back on error.

Example pattern using PDO (safe, concise, and different from the snippets already posted):

$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT INTO customer_order (quantity, fast_image, menu_name, price) VALUES (?, ?, ?, ?)');

foreach ($_SESSION['cart'] as $item) {
    $stmt->execute([
        (int)$item['quantity'],
        $item['fast_image'],
        $item['menu_name'],
        (float)$item['price']
    ]);
}

$pdo->commit();

Quick troubleshooting and hardening checklist: run var_export($_SESSION['cart'], true) to confirm structure; ensure session_start() runs before any output; prefer associative keys ($item['quantity']) for clarity; cast types for numeric fields; enable exceptions for DB errors and wrap inserts in a transaction; and migrate away from mysql_query() (see mysql_query deprecation) or use PDO prepared statements and session_start documentation for reference.

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

$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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.