So I have this script which displays multiple user’s stores, so it needs to create a uniquely identifiable cart session so that the user can visit several different stores without their cart being duplicated on someone else’s store…
I’m just at loose ends with this and it seems to recreate the same basket on different stores!
Any ideas? Greatly appreciated!
<?php
session_start();
include("dbstore.php");
$storeurl = addslashes($_GET['url']);
$getstore = mysql_query("SELECT * FROM `store` WHERE `url` = '$storeurl'") or die(mysql_error());
$store = mysql_fetch_array($getstore);
$current = time(); //Current timestamp
// Grab shopping cart session if it exists
if(isset($_SESSION["cart_id_set_$store[id]"])) {
$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '".$_SESSION['cart_id_$store[id]']."'") or die(mysql_error());
$basket = mysql_fetch_assoc($getbasket);
$get_basket_items = mysql_query("SELECT * FROM `store_basket_items` WHERE `basketid` = '$basket[id]'") or die(mysql_error());
$total_items = mysql_num_rows($get_basket_items);
$basket_total = mysql_query("SELECT SUM(price) AS price FROM store_basket_items WHERE basketid = '$basket[id]'");
$total = mysql_fetch_array($basket_total);
$total_price_format = number_format($total[price], 2, '.', ',');
echo"<div id=\\"basket\\">$total_items Items - £$total_price_format <u>Check Out</u></div>";
// Create shopping cart session if it does not exist yet..
} else {
$current2 = time(); //Current timestamp
$countcarts = mysql_query("SELECT MAX(id) AS maximumId FROM `store_basket") or die(mysql_error());
$totalCarts = mysql_fetch_object($countcarts);
$new_totalCarts = ($totalCarts->maximumId+1);
$createcart = mysql_query("INSERT INTO `store_basket` (`id`, `timestamp`) VALUES('" . mysql_real_escape_string($new_totalCarts) . "', '" . $current2 . "')") or die(mysql_error());
$basketid = mysql_insert_id();
$_SESSION["cart_id_$store[id]"] = $basketid;
$_SESSION["cart_id_set_$store[id]"] = 'yes';
echo"<div id=\\"basket\\">0 Items - £0.00 <u>Check Out</u></div>";
}
?>
I’m now trying this code out, but it results in displaying BOTH print_r’s as if it thinks it is set and isn’t, and the basket entries into SQL aren’t even turning up!
So confusing!!
<?php
session_start();
include("dbstore.php");
$storeurl = addslashes($_GET['url']);
$getstore = mysql_query("SELECT * FROM `store` WHERE `url` = '$storeurl'") or die(mysql_error());
$store = mysql_fetch_array($getstore);
$storeId = $store['id'];
$current = time(); //Current timestamp
// Grab shopping cart session if it exists
if(!isset($_SESSION['store'][$storeId]['cart'])) {
$current2 = time(); //Current timestamp
$countcarts = mysql_query("SELECT MAX(id) AS maximumId FROM `store_basket") or die(mysql_error());
$totalCarts = mysql_fetch_object($countcarts);
$new_totalCarts = ($totalCarts->maximumId+1);
$createcart = mysql_query("INSERT INTO `store_basket` (`id`, `timestamp`) VALUES('" . mysql_real_escape_string($new_totalCarts) . "', '" . $current2 . "')") or die(mysql_error());
$basketid = mysql_insert_id();
$_SESSION['store'][$storeId]['cart'] = $new_totalCarts;
print_r($_SESSION['store'][$storeId]['cart']);
}
$sessionvalue = $_SESSION['store'][$storeId]['cart'];
$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '$sessionvalue'") or die(mysql_error());
$basket = mysql_fetch_assoc($getbasket);
$get_basket_items = mysql_query("SELECT * FROM `store_basket_items` WHERE `basketid` = '$basket[id]'") or die(mysql_error());
$total_items = mysql_num_rows($get_basket_items);
$basket_total = mysql_query("SELECT SUM(price) AS price FROM store_basket_items WHERE basketid = '$basket[id]'");
$total = mysql_fetch_array($basket_total);
$total_price_format = number_format($total[price], 2, '.', ',');
print_r($_SESSION['store'][$storeId]['cart']);
echo"<div id=\\"basket\\">$total_items Items - £$total_price_format <u>Check Out</u></div>";
?>
The problem seems to be that it doesn’t set the session (that or be able to read it), as it defaults to the else statement and displays “not yet set” which I told it to within the echo on the else statement
Mainly your problem is… $store[id] is not going to be parsed in your session variable correctly with the way you are doing it. Either rename like this:
Also… if each store is going to have a unique URL, as long as you clean that URL variable, i would use that instead of the store id… since that is a unique identifier.
// Rough
$storeUrl = htmlentities($_GET['url']);
// WOuld probably want to check to see if it is a valid url..
$_SESSION['store'][$storeUrl]['cart'];