Hi, i am trying to add a very simple shopping cart script to my site, and the session is simply used to keep the contents of the shopping cart, yet its not working properly!
Ok ignoring the layout issues with the cart here is the issues.
Only two links to the same script
First one is a link to the shopping cart with an action to add the item ID to it.
http://www.heliuk.co.uk/index.php?n=pages/shop-cart-action&id=1&action=add
This works, it adds the item to the session "cart", if that's successful it then shows the cart from the session.
But if i just go to the shopping cart with no actions (so just to view it) it shows no items
http://heliuk.co.uk/index.php?n=pages/shop-cart-action
And what this is doing is checking it the cart is empty, if not show the cart, if it is then show nothing, which is what happening. But if you go back to the first link and add the item again, it is adding it on to the cart so the session is there and working? I don't understand what's happening!
Here the shopping cart page.
<?php
error_reporting(E_ALL ^ E_NOTICE);
$product_id = $_GET["id"]; //the product id from the URL
$action = $_GET["action"]; //the action from the URL
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) { //decide what to do
case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
?>
<table class='main' cellspacing='1' cellpadding='4'>
<tr class='head'>
<td class='head' colspan='2'>HeliUK Shop - Your Shopping Cart</td>
</tr>
<tr>
<td style='Text-align:left;' class='con1' colspan='2'>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<?php
error_reporting(E_ALL ^ E_NOTICE);
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table border=\"1\" padding=\"0\" width=\"100%\">"; //format the cart using a HTML table
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT title, description, price FROM items WHERE id = %d;",
$product_id);
$result = mysql_query($sql);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($name, $description, $price) = mysql_fetch_row($result);
$line_cost = $price * $quantity; //work out the line cost
$total = $total + $line_cost; //add to the total cost
echo "<tr>";
echo "<td style=\"border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\" width=\"145\">";
echo "<font face=\"Tahoma\" size=\"2\">$quantity</font></td>" ;
echo "<td style=\"border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\" width=\"693\">";
echo "<font face=\"Tahoma\" size=\"2\">$name</font></td>" ;
echo "<td style=\"border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\" width=\"162\">";
echo "<font face=\"Tahoma\" size=\"2\">$line_cost</font></td>" ;
echo "<td style=\"border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\">";
echo "<p align=\"center\"><a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">" ;
echo "<img border=\"0\" src=\"empty-cart.jpg\" width=\"24\" height=\"24\"></a></td>" ;
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"0\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"0\" align=\"right\"><a href=\"$_SERVER[php_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
echo "</tr>";
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shopping cart.";
}
?>
</table>
</td>
</tr>
</table>
<?
//function to check if a product exists
function productExists($product_id) {
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM items WHERE id = %d;", $product_id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>