Hey, I have a fully-functioning shopping cart which is going through to a checkout perfectly fine. That is, unless its the very first time that a user has accessed the site.
If its the first time a session id is being registered for that person, the the details add to the shopping cart fine, add to my "shopping" mysql db table fine.........but in my next page, Checkout.php, they are blank.
However, if I return back to one of my shopping pages and add a product to the cart(bear in mind it adds this item to the cart in the same fashion as the first time round).....the correct details are indeed passed to the Checkout.php page fine.
This is pretty damn annoying, as you can imagine.....I have my values going into my shopping db perfectly....as long as its not the first time passing them to Checkout.php, then all my scripts work perfectly and I pass the relevant details into my orders table.
If anybody can shed any light on this problem, it'd be great.
This is my "Showcart.php" script, which calls a shoppingCart.php file which contains all my functions for adding, deleting etc from the cart:
<?
// Cart Cookie Creator //////////////////////////
if(!isset($HTTP_COOKIE_VARS['cart_id'])) {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 14400);
} else {
$cart_id = $HTTP_COOKIE_VARS['cart_id'];
}
/////////////////////////////////////////////////
// Make the Cart Object /////////////////////////
require_once("shoppingcart.php");
$cart = new cart($cart_id);
/////////////////////////////////////////////////
// Select Action ////////////////////////////////
switch($_GET['a']){
case "add":
if(isset($_POST['p']) and isset($_POST['q'])) $cart->add_item($_POST['p'],$_POST['q']);
break;
case "update":
$cart->modify_quantity($_POST["p"],$_POST["q"]);
break;
case "delete":
$cart->delete_item($_POST['p']);
break;
case "clear":
$cart->clear_cart();
break;
}
//Some code here which writes to the cart display
//some formatting
<?
echo $cart_tbl;
$t = $cart->cart_total();
$qty = $cart->quant_items();
echo "
<p>
<!-- Begin Mini Cart Display -->
<!-- Note: Put this mini cart on all ecommerce pages except showcart.php. -->
<!-- It is just here for demonstration and testing purposes -->
<table summary='Your shopping cart' id='minicart'>
<tr>
<td class='minihead' colspan='2'>Your Shopping bag</td>
</tr>
<tr class='minicontent'>
<td>" . $qty . " items</td><td>" . $t . "</td>
</tr>
</table>
<!-- End Mini Cart Display -->
</p>\n\n\n";
?>
This is an example of one of my product pages, which calls an include on the Showcart.php:
<?
session_start();
session_register('productCode');
session_register('productName');
session_register('price');
session_register('productSpace');
$shopping = $_SESSION['shopping'];
//some db queries non-relevant to question
//some formatting
Product Category : <?php echo $productCategory ?>
<br>
Product Name : <?php echo $productName ?>
<br>
Product Code : <?php echo $productCode ?>
<br>
Product Price : <?php echo $productPrice ?>
<br>
<?php if($productStockLevel > 0) echo '<img src="tick.gif" width="16" height="16" hspace="5" alt="">In Stock <br />';else echo '<img src="red-x.gif" width="16" height="16" hspace="5" alt="">Not in Stock <br />';?>
Product Description : <?php echo $productDescription ?>
<br></p>
<br>
<br>
<p><form action="<?echo $productSpace ?>.php?a=add" method="post"><input type="submit" value="add item" /><input type="hidden" value="<?echo $productCode ?>" name="p" /><input type="hidden" value="1" name="q" /></form></p>
</DIV>
<br><br><br><br>
<div class=cart>
<?
require_once("../showcart.php");
/////////////////////////////////////////////////
?>
</div>
....and this is the Checkout.php file, which takes the values from the product page's cart succesfully every time....except for the very first time a user accesses it:
<?php session_start();
session_register('CardNumber');
session_register('CardType');
$_SESSION['CardNumber'] = $_POST['CardNumber'];
$_SESSION['CardType'] = $_POST['CardType'];
?>
<?
$_SESSION['sessionID'] = $cart_id;
// Make the Cart Object /////////////////////////
include("showcart.php");
/////////////////////////////////////////////////
?>
//some formatting and a form to take cc details and validate them