0

How do I store my data in session array into my database ? Upon entering the product and its quantity, the user will be directed to a checkout page. In the checkout page, upon filling up the necessary information in a form, I am trying to use a button to submit the session array into my database.

My current code:

<?php
    session_start();
    $array = $_SESSION['shopping_cart'];  
    $connect = mysqli_connect('localhost', 'root', '', 'table');
    ?>
    $sql = "INSERT INTO customer_order (productName, quantity, totalPrice, pax)";
    foreach($array as $product){
       $sql = "INSERT INTO p2_5.customer_order (productName, quantity, totalPrice, pax)";
       $sql .= " VALUES ('{$product['name']}', '{$product['quantity']}', '{$product['price']}', '{$product['pax']}')";
       if ($connect->query($sql)) {
          $errorMsg = "Connection failed: " . $connect->connect_error;
          $success = false;
   } 
}

However, upon clicking on the button, I am able to store client information into my database, but I would have an PHP error message Undefined index: name, Undefined index: quantity, Undefined index: price, Undefined index: pax ?

enter image description here

9
  • 1
    Can do it a a JSON String json_encode() or using serialize() Commented Nov 15, 2019 at 12:28
  • 1) you're closing php tag with ?>. 2) you're passing elements from $array without referencing to their index, I mean without $array[0] or $array[1] Commented Nov 15, 2019 at 12:28
  • What order information are you missing? Commented Nov 15, 2019 at 12:29
  • “What did I do wrong ?” - looks like you are developing without having proper PHP error reporting enabled, otherwise PHP should have given you notices about how you are trying to access array elements that aren’t actually there. So please go and enable it first of all now! Commented Nov 15, 2019 at 12:30
  • Oh I see now. You need to add a loop to your code. Looping over the $array Commented Nov 15, 2019 at 12:31

1 Answer 1

4

You're passing elements from $array without referencing to their indexes, I mean without $array[0] or $array[1].

You need to add a loop like below:

foreach($array as $product){
   $sql = "INSERT INTO customer_order (productName, quantity, totalPrice, pax)";
   $sql .= " VALUES ('{$product['name']}', '{$product['quantity']}', '{$product['price']}', '{$product['pax']}')";
   if ($connect->query($sql)) {
       $errorMsg = "Connection failed: " . $connect->connect_error;
       $success = false;
   } 
   if ($errorMsg != "Connection failed: ") {echo $errorMsg; $connect->close(); return;}
}

This will allow you to save each row data from shopping_cart.

Sign up to request clarification or add additional context in comments.

8 Comments

sorry, however when I check my database, nothing is being stored
@JohnWick, show me echo $sql; return;. Put it before if statement. This will make the first query string and you'll see what you're sending to DB.
INSERT INTO root.customer_order (productName, quantity, totalPrice, pax) VALUES ('Seafood Set', '8', '250', '90')
@JohnWick, try to erase p2_5.(or root.) and try again
@JohnWick, hah, then add ' around each column name -> ('productName', 'quantity', 'totalPrice', 'pax')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.