Jump to content

save session shopping cart data into database


benh5851

Recommended Posts

ok, well I don't know if your current script is going to particularly work for that sought of thing  :-\ I think maybe make a new page, and set it up like the following:

<?php
// session variables into common variables
$cart = $_SESSION["cart"];
$item = $_SESSION["item"];
// or whatever your session variables are
// then run a INSERT INTO query for all of them into the database with all the $rs variables of course
$sql = "INSERT INTO orders (cart,item) VALUES ('$cart','$item')";
// then once you have done all the inserts have it redirect to whatever page you want
header("Location: page.php");

 

I think the fact that you have an Array() inside a session variable is whats stuffing things up, I've never worked with Arrays inside session variables so I dont know how to grab the data from inside the array when its in a session variable, but I think the way to get this to work is to select each separate bit of data from inside the array and then insert it into the database and this will fix the problem.

 

I made a function to do a similar thing like this before, so maybe you might be able to modify it to select data from your array inside your session variable:

<?php
// Select 1 field from an Array
function select_array($array,$id,$field) {

     return $array[$id][$field]; // the format is Array, ID of array your selecting within the main array, the field you
                                        // select from inside the array.
                                        // you need to somehow change it to grab the array from the session var and then   
                                        // select all the fields within the array and then put them into common variables to 
                                        // insert into the database

}

 

Regards ACE

not particularly lol, Its just a dead end  :D

I'm gonna have a read through my PHP textbooks and see if I can find anything on arrays in session variables.

 

EDIT: just got a idea, why not try a foreach statement? lets try a few different things with a foreach statement and see what we come up with.

 

<?php
session_start();

$common_variable = $_SESSION['cart'];
foreach($common_variable as $key => $val) {
echo $key;
echo "<hr>";
echo $val;
}

 

give that a try and see if it displays anything.

hmm ok, back to the drawing board lol  :D

 

<?php
session_start();

$common_variable = $_SESSION['cart'];
foreach($common_variable as $key => $val) {
echo $key;
echo "<hr>";
echo $val;
echo "<hr>";
echo count($common_variable);
echo "<br>";
echo count($key);
echo "<br>";
echo count($val);
}

 

see what that says.

Great --- this works but i think a little work is needed.

 

<?php

session_start();

 

$rs = mysql_connect(  );

$rs = mysql_select_db(  );

 

 

$cart = $_SESSION['cart'];

 

foreach($cart as $items);

 

 

$sql = "INSERT INTO orders (items) VALUES ('$items')";

$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

 

 

header("location:pay.php");

 

?>

 

but i only get the id of the last product added to the cart!

try something like this after the foreach statement:

<?php
$item_id = $items['id'];

 

and you can do that for each of the fields in the array, and then insert each common variable, eg $item_id into the database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.