hi,
i have a shopping cart on my site that i would like to be saved into a data base so i could view the order in a backend application and show it to the customer in a previous orders section.

the code i have so far to add the cart to the session....

<? session_start();

if ( !isset( $_SESSION["cart"] ) ) { 
    $_SESSION["cart"] = array(); 
} 

$id = $_POST["id"]; 
$_SESSION["cart"][$id] = $id; 

header("location:index.php"); 

?> 

The total is worked out on the cart page...

$sub[]=$info['price'];
}

$subtotal = array_sum($sub);
$postage = 3.50;
$item_total = array_sum($sub);
$final = $item_total + $postage;
echo number_format($final, 2, '.', ','); 
}

This all works so how can i save the data?
i have made it so no address is needed as a loginform is shown first. the their address is shown then i need a complete order button to add the cart contents to the database???

many thanks
ben

    Since your cart array that you store in session is an array, you'll need to use [man]serialize/man on it. This will turn it into a simple string which can be stored in a database.

    to store it in a database you will need a table...let's assume you have a table called 'user_carts' which has two fields:

    user_carts
      id - an integer field that autoincrements
      cart_contents - a text field
    

    then you can store a cart in the db like this:

    $serialized_cart = serialize($_SESSION["cart"]);
    $sql = "INSERT INTO user_carts (cart_contents) VALUES ('" . mysql_real_escape_string($serialized_cart) . "')";
    mysql_query($sql)
      or die("Query to store cart failed");
    

    to retrieve the cart, you can do this:

    $cart_id = intval($_GET['id']);
    $sql = "SELECT cart_contents FROM user_carts WHERE id=" . $cart_id;
    $result = mysql_query($sql)
      or die("Query to retrieve cart failed");
    if (mysql_num_rows($result) < 1) {
      die("Cart not found !");
    }
    $row = mysql_fetch_assoc($result)
    $cart = row["cart_contents"];
    

      and of course
      use [man]unserialize[/man] on that string
      to get it back into $_SESSION

      session_start(); // dont forget!
      
      // the id of current user
      $cart_id = intval($_GET['id']);
      
      $sql = "SELECT cart_contents FROM user_carts WHERE id=" . $cart_id;
      $result = mysql_query($sql)
        or die("Query to retrieve cart failed");
      if (mysql_num_rows($result) < 1) {
        die("Cart not found !");
      }
      
      $row = mysql_fetch_assoc($result);
      
      // serialized = array in a series of characters forming a string
      // we convert serialized STRING back into ARRAY
      $cart = unserialize( row["cart_contents"] ); 
      
      // logged in customer/user gets back his cart, with contents from last visit
      // we make $_SESSION array = $cart array
      $_SESSION['cart'] = $cart;

        yeah sorry! thanks for watching my back halo!

          sneakyimp wrote:

          yeah sorry! thanks for watching my back halo!

          easy to finish what somebody else did the main thinking on

          sorry, for not mentioning your name in my post
          ... I usually refer to other member(s):

          using sneakyimp code, we can do this:
          etc
          etc

          I think this is a nice gesture 🙂
          - Glory to whom glory belongs

          Regards

            in my database it just shows n;
            any one know why also when i try to show the cart on a blank page i get...

            Parse error: syntax error, unexpected '[' in /mounted-storage/home43c/sub002/sc32535-PERI/v2/view.php on line 27

            Help???

            Thanks a lot ;]

              hi ,

              i have a running shopping cart and i need to save the data in the cart to a orders database. the items in the cart are stored in a session by their id and then are displayed in the cart by sending a query to the items database finding their name, price and product code.

              how can i put the carts data into a db called orders.

              the items are stored into the session by...

              <? session_start();
              
              if ( !isset( $_SESSION["cart"] ) ) { 
                  $_SESSION["cart"] = array(); 
              } 
              
              $id = $_POST["id"]; 
              $_SESSION["cart"][$id] = $id; 
              
              header("location:index.php"); 
              
              ?> 

              thanks a lot
              ben 🙂

                I merged your two threads together as I could see no reason for the duplication.

                  Write a Reply...