Hi all
I am in the middle of creating a shopping site for a friend. Everything is going well except for one thing. For some reason when I have two items in a cart and one item is deleted the item itself is removed but the total still counts it as being there.
The Site in Question
An example:
Click to buy 10 "Brunello" and then return and buy 10 "Giraffe". Now go to the checkout and remove the 10 Brunello. You will notice that the cart still shows $24.
I have placed the two scripts below the first is the Checkout script and the second the Remove script.
Strangest thing is sometimes it actually works.
Checkout.php
// Make a MySQL Connection
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("db122078148") or die(mysql_error());
$item = $_POST["Name"];
$price = $_POST["Price"];
$quantity = $_POST["Quantity"];
$discount = $_POST["Discount"];
$catagory = $_POST["Catagory"];
// Check if cart is set
if(isset($_SESSION['cart']))
{
// If item exists add 1 to quantity
if(array_key_exists($item,$_SESSION['cart']))
{
$_SESSION['cart'][$item][0] = $_SESSION['cart'][$item][0] + $quantity;
}
// If item does not exist create it
else
{
$_SESSION['cart'][$item] = array($quantity,$price,$discount);
}
}
// If not create cart
else
{
$_SESSION['cart'] = array( $item => array($quantity,$price,$discount));
}
//// Output the cart ///////////////////////////////////////////////////////////////
$totalcost = 0;
$cartitems = '';
foreach($_SESSION['cart'] as $key => $value)
{
$id = $key ;
$q = $_SESSION['cart'][$id][0];
$p = $_SESSION['cart'][$id][1];
$d = $_SESSION['cart'][$id][2];
if($_SESSION['cart'][$id][2] == 1)
{
if ($_SESSION['cart'][$id][0] >= 5)
{
$div = floor($_SESSION['cart'][$id][0] / 5);
$total = $div * 5;
$total += ($_SESSION['cart'][$id][0] % 5) * $_SESSION['cart'][$id][1];
}
else {$total = $_SESSION['cart'][$id][0] * $_SESSION['cart'][$id][1];}
}
if($_SESSION['cart'][$id][2] == 2)
{
if ($_SESSION['cart'][$id][0] >= 5)
{
$div = floor($_SESSION['cart'][$id][0] / 5);
$total = $div * 7;
$total += ($_SESSION['cart'][$id][0] % 5) * $_SESSION['cart'][$id][1];
}
else {$total = $_SESSION['cart'][$id][0] * $_SESSION['cart'][$id][1];}
}
if($_SESSION['cart'][$id][2] == 3)
{
if ($_SESSION['cart'][$id][0] >= 5)
{
$div = floor($_SESSION['cart'][$id][0] / 5);
$total = $div * 9;
$total += ($_SESSION['cart'][$id][0] % 5) * $_SESSION['cart'][$id][1];
}
else {$total = $_SESSION['cart'][$id][0] * $_SESSION['cart'][$id][1];}
}
$totalcosts = $totalcost + $total;
$totalcost = number_format($totalcosts, 2);
$cartitems = $cartitems.' '.$id.'-'.$q;
If($id != "")
{
echo "<tr >
<td class=Details>$id</td>
<td class=Details>$q</td>
<td class=Details>£$p</td>
<td class=Details>$d</td>
<td class=CartTitle><form name=form1 method=POST action=Remove.php>
<input name=Submit type=submit class=CatSelectButton value=Remove>
<input name=Id type=hidden id=Id value=$id>
</form></td>
<td> </td>
</tr>";
}
}
Remove.php
session_start();
$item = $_POST["Id"];
unset($_SESSION['cart'][$item]);
header('Location: Checkout.php');
Please help anyone. I am a little behind schedule.
Thanks