title: PHP Unable to increment any value but the last added to the array
body: <p>I am using incrementing/decrementing to change the quantity demanded of items in my cart. However, this only seems to be working for the last value/row added to the array. e.g. if I add 1 item to the cart, incrementing ($_SESSION['cart'][$comic_id]++) works fine. However if I add a second item to the cart, incrementing only works for the latest added item and not the first added, and the same can be said for any additional added items. </p>
  
  <p>Can someone please indicate where I have gone wrong and help me fix my code please?</p>
  
  <p>I am echoing using the $value at the end contained within a foreach loop.</p>
  
  <p>Here is my code (cart.php):</p>
  
  <pre><code>&lt;?php 
  if(!isset($_SESSION)) {
       session_start();
  }
  $total = 0;
  $page = 'cart';
  
  //Database connection info (Using MySQLi) removed for security
  
  if(isset($_GET['comic_id'])){ 
      $comic_id = $_GET['comic_id'];
  } else {
      $comic_id = NULL;}
  
  $action = $_GET['action'];
  
  if($comic_id &amp;&amp; !productExists($comic_id)) {
      die("We apologise, but we can't seem to find the comic you were looking for!!!");
  }
  
  switch($action) {
      case "add":
          if (isset($_SESSION['cart'][$comic_id])) {
              $_SESSION['cart'][$comic_id]++;
  
          } else {
              $_SESSION['cart'][$comic_id] = 1;
              }
      break;  
      case "remove":
          if (isset($_SESSION['cart'][$comic_id])) {
              $_SESSION['cart'][$comic_id]--;}
          if($_SESSION['cart'][$comic_id] == 0) unset($_SESSION['cart'][$comic_id]);
      break;  
      case "empty":
          unset($_SESSION['cart']); 
          session_destroy();
      break;  
  }
  
  require('header.php');
  require('sidebar.php');
  
  if (isset($_SESSION['cart'][$comic_id])){
  
      echo "&lt;table border=\"0\" padding=\"10\" width=\"80%\"&gt;";
      echo "&lt;td colspan=\"1\" align=\"left\"&gt;&lt;a href=\"title.php\"&gt;Continue Shopping&lt;/a&gt;&lt;/div&gt;";
      echo "&lt;td colspan=\"6\" align=\"right\"&gt;&lt;a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Crystal Fusion: Are you sure you wish to empty your cart?');\"&gt;Empty Cart&lt;/a&gt;&lt;/td&gt;";                      
      echo "&lt;tr height=\"20px\"&gt;";
      echo "&lt;tr height=\"20px\"&gt;";
      echo "&lt;td align=center&gt;Image&lt;/td&gt;&lt;td align=center&gt;Title&lt;/td&gt;&lt;td align=center&gt;Description&lt;/td&gt;&lt;td colspan=3 align=center&gt;Copies (+/-)&lt;/td&gt;&lt;td align=center&gt;Price&lt;/td&gt;";
      echo "&lt;tr height=\"20px\"&gt;";
  
  
      foreach($_SESSION['cart'] as $comic_id =&gt; $qty) {   
  
          $sql = sprintf("SELECT title, description, cost, image_thumbnail
                  FROM comic 
                  WHERE comic_id = %d;",$comic_id);
  
          $result = mysql_query($sql);
  
          if(mysql_num_rows($result) &gt; 0) {
  
              list($name, $description, $price, $image_thumbnail) = mysql_fetch_row($result);
  
              $cost = $price * $qty; 
              $total = $total + $cost;
  
              $cost = number_format($cost,2);
              $total = number_format($total,2);
              $description =  substr($description, 0, 250);
  
              echo "&lt;br&gt;&lt;tr&gt;";
              echo "&lt;td width=\"10px\" align=\"center\"&gt;&lt;img height=100 align=center src=\"$image_thumbnail\"&gt;";
              echo "&lt;td align=\"center\"&gt;$name&lt;/td&gt;";
              echo "&lt;td width=\"40%\" align=\"center\"&gt;$description...&lt;a href=comic_dyn.php?comic_id=$comic_id&gt;More Info&lt;/td&gt;";
              echo "&lt;td width=\"30px\" align=\"center\"&gt;&lt;a href=\"$_SERVER[PHP_SELF]?action=add&amp;comic_id=$comic_id\"&gt;+&lt;br&gt;&lt;/a&gt;&lt;td align=\"center\"&gt;$qty &lt;td width=\"20px\" align=\"center\"&gt;&lt;a href=\"$_SERVER[PHP_SELF]?action=remove&amp;comic_id=$comic_id\"&gt;-&lt;/a&gt;&lt;/td&gt;";
              echo "&lt;td align=\"right\"&gt;$$cost&lt;/td&gt;";
              echo "&lt;/tr&gt;";           
          }
  
      }
  
      echo "&lt;br&gt;&lt;tr&gt;&lt;tr height=100px&gt;";
      echo "&lt;td&gt;&lt;td&gt;&lt;td colspan=\"4\" align=\"right\"&gt;Total:&lt;/td&gt;";
      echo "&lt;td width=\"60px\" align=\"right\"&gt;$$total&lt;/td&gt;"; 
      echo "&lt;tr&gt;&lt;td colspan=\"7\" align=\"right\"&gt;&lt;a href=\"checkout_html.php\"&gt;Proceed to Checkout&lt;/a&gt;";
      echo "&lt;tr height=\"50px\"&gt;";
      echo "&lt;/table&gt;";
  }else{
  
      echo "Your cart is currently empty.";
      echo "&lt;br&gt;&lt;br&gt;&lt;td colspan=\"1\" align=\"left\"&gt;&lt;a href=\"title.php\"&gt;Continue Shopping&lt;/a&gt;&lt;/div&gt;";
  
  }
  
  $_SESSION['invoice'][$comic_id]=$name . " " . $qty . " " . $price;
  
  foreach ($_SESSION['invoice'] as $value) {
      echo $value."&lt;br&gt;";}
  
  function productExists($comic_id) {
          $sql = sprintf("SELECT * FROM comic WHERE comic_id = %d;", $comic_id);
      return mysql_num_rows(mysql_query($sql)) &gt; 0;
      }
  ?&gt;
  </code></pre>