I made a shopping cart a while back. Here is the code. Posting it here maybe you could get an idea on how to do your cart (this is a very simple example and only for test... needs a whole lot more work to get it done to production level). This is just to give you an idea of what I've done and maybe on how you could fix your problem. Of course you'd want to validate user input before running them through the class.
class shoppingCart
{
var $product = array();
public function add($pid, $qt)
{
$in_cart = false;
if($qt < 1)
{
throw new exception("You're not adding any amount of the item.");
}
if(isset($_SESSION['shopping_cart']))
{
$items = $this->showSaved();
foreach($items as $p_id => $quantity)
{
if($pid == $p_id)
{
$quantity = $qt + $quantity;
$in_cart = true;
}
$product[$p_id] = $quantity;
}
}
if($in_cart == false)
{
$product[$pid] = $qt;
}
$this->product = $product;
$this->save();
}
public function remove($pid, $qt = 0)
{
if(isset($_SESSION['shopping_cart']))
{
if(isset($_SESSION['shopping_cart'][$pid]))
{
if($qt !== 0)
{
if($qt >= $_SESSION['shopping_cart'][$pid])
{
throw new exception("Trying to remove {$qt} of a product when you only have {$_SESSION['shopping_cart'][$pid]} of it!");
return false;
}
$_SESSION['shopping_cart'][$pid] = $_SESSION['shopping_cart'][$pid] - $qt;
return true;
}
else
{
unset($_SESSION['shopping_cart'][$pid]);
return true;
}
}
else
{
throw new exception("Item doesn't exist in the cart. Nothing to remove.");
}
}
else
{
throw new exception("The cart is empty. Nothing to remove.");
}
}
function showSaved()
{
return (array) $_SESSION['shopping_cart'];
}
function save()
{
$_SESSION['shopping_cart'] = $this->product;
}
function clearCart()
{
$_SESSION['shopping_cart'] = array();
}
}
session_start();
$cart = new shoppingCart();
?>
<p>The Shopping Cart:</p>
<p>To <strong>add</strong> to cart, simply select the product you want to add and type in the quantity and click 'Add'.</p>
<p>To <strong>clear</strong> the cart, simply click 'Clear Cart'.</p>
<p>To <strong>remove</strong> from cart, simply select the product and type in the quantity to remove (or leave blank to remove the item entirely) and click 'Remove from Cart'.</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<p>Product: <select name="prods">
<option value="1">Pair or Socks</option>
<option value="2">Pair of Underwear</option>
<option value="3">The House on the Hill</option>
<option value="4">Laundry detergent</option>
<option value="5">Juice Boxes</option>
<option value="6">PHP Dev's Book</option>
</select><br />
Quantity: <input type="text" name="qt" size="10" /><br />
<input type="submit" name="submit" value="Add" />
<input type="submit" name="clear" value="Clear Cart" />
<input type="submit" name="remove" value="Remove from Cart" /><p></form>
<pre>
<pre>
try
{
if(isset($_POST['submit']))
{
$cart->add($_POST['prods'], $_POST['qt']);
print_r($cart->showSaved());
}
if(isset($_POST['clear']))
{
$cart->clearCart();
print_r($cart->showSaved());
}
if(isset($_POST['remove']))
{
$qt = (integer) (isset($_POST['qt']) && !empty($_POST['qt'])) ? $_POST['qt'] : 0;
$cart->remove($_POST['prods'], $qt);
print_r($cart->showSaved());
}
}
catch( exception $e )
{
echo "<pre>";>
echo $e->getMessage() . ' In ' . $e->getFile() . ':' . $e->getLine() . '<br /><br />';
echo "Stack Trace<br />";
echo $e->getTraceAsString();
echo "</pre>";
}
?>