0

I was trying to insert multiple items from my cart to my database(mysql). The code below processes only one row. Can you guys help me? What code should I use to execute multiple rows in one process? Thank you in advance.

if (isset($_POST['cart_btn'])) {
    $product_id    = $_POST['product_id'];
    $product_name  = $_POST['product_name'];
    $filename      = $_POST['filename'];
    $price         = $_POST['price'];
    $quantity      = $_POST['quantity'];
    $subtotal_item = $_POST['subtotal_item'];
    $subtotal      = $_POST['subtotal'];
    $total         = $_POST['total'];
    $shipping_fee  = $_POST['shipping_fee'];

    if (!isset($errormessage)) {
        $order_id = time().'-'.mt_rand();
        $stmt     = $db->prepare('INSERT INTO cart(order_id,product_id,product_name,filename,price,quantity,subtotal_item,subtotal,total,shipping_fee) VALUES(:uorder_id,:uproduct_id,:uproduct_name,:ufilename,:uprice,:uquantity,:usubtotal_item,:usubtotal,:utotal,:ushipping_fee)');

        $stmt->bindParam(':uorder_id', $order_id);
        $stmt->bindParam(':uproduct_id', $product_id);
        $stmt->bindParam(':uproduct_name', $product_name);
        $stmt->bindParam(':ufilename', $filename);
        $stmt->bindParam(':uprice', $price);
        $stmt->bindParam(':uquantity', $quantity);
        $stmt->bindParam(':usubtotal_item', $subtotal_item);
        $stmt->bindParam(':usubtotal', $subtotal);
        $stmt->bindParam(':utotal', $total);
        $stmt->bindParam(':ushipping_fee', $shipping_fee);

        if ($stmt->execute()) {
            $successmessage = "Please wait, processing your order...";
            header("refresh:5;checkout.php");
        } else {
            $errormessage = "process on error....";
        }
    }
}
Ron Gus
  • 11
  • 2
  • `VALUES(...), (...), (...)...` or loop through values using `while`, `do...while`, `for`, `foreach`, etc. and run the `INSERT` query upon each iteration – ctwheels Jun 28 '17 at 18:34
  • possible duplicates of https://stackoverflow.com/questions/1176352/pdo-prepared-inserts-multiple-rows-in-single-query – Blkc Jun 28 '17 at 18:35
  • 4
    Possible duplicate of [PDO Prepared Inserts multiple rows in single query](https://stackoverflow.com/questions/1176352/pdo-prepared-inserts-multiple-rows-in-single-query) – Norbert Jun 28 '17 at 19:43

0 Answers0