Page 1 of 1

Removal of items from cart removes more than I want!

Posted: Wed Mar 09, 2011 6:44 pm
by Cyberen
Whenever I have multiple items in this shopping cart, if I don't remove the items starting from the latest-added item down (the highest rem&id=Number) , it removes others I don't ask for. What can I do to fix the removal of these items? I think it might be a glitch in the code.

Here's the button that removes an item from the cart:

Code: Select all

<? echo '<a class="sub" href="viewcart2.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '">Remove</a> '; ?> 
Here's the function that removes the item from the cart. Is not unsetting all the $_session values a bad thing? It certainly doesn't remove it from the $_SESSION array if you print one out at the bottom.

Code: Select all

<?
ini_set('display_errors','Off'); 
include 'db.php';

session_start();
header("Cache-control: private");
$num_items=0;
for($i=0;$i<sizeof($_SESSION['cart']);$i++){
 if(($_GET['action']=='rem'||$_GET['action']=='rev') && $_GET['id']==$i){

  if($_GET['action']=='rev'){
   $val=$_SESSION['cart'][$i];
   $items= split("\|\|",$val);
  }
  unset($_SESSION['cart'][$i]); 
  if($_GET['action']=='rev'){
   header("Location: pro.php?id=" . $items[0]);
   exit;  
  }
 }
 $val=$_SESSION['cart'][$i];
 $items= split("\|\|",$val);
 if($items[0]!=""){ 
  $num_items++;
 }
}
?> 
And here's the loop that creates items from the database:

Code: Select all

<?PHP
$total=0;
for($i=0;$i<sizeof($_SESSION['cart']);$i++){

$val=$_SESSION['cart'][$i];
$items= split("\|\|",$val);
if($items[0]!=""){
$query = "SELECT *,category from products,categories where categories.id=products.pid and products.id=" . $items[0];
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
?>
<?= stripslashes($line['name'])
}
?>
I have the same problem on two different versions of the webpage. The main difference is that viewcart5.php adds more $_SESSION values to the array, and I thought by unsetting them like I did with $_SESSION['cart'] the problems would go away, but the same problem involving removing items persists.
I don't know how common this problem is but I will provide any information I can. Should I just post the whole page now?