title: Adding item to shopping cart using php and html
body: <p>I am attempting to implement a shopping cart using php, and html. The problem I am having is storing the id of the product to be stored using a session. The following is the code that I have at the moment:</p>
  
  <pre><code>&lt;?php
  session_start();
  ?&gt;
  //There are three HTML Forms that take and display input, and output.
  
  &lt;?php
  $_SESSION['cart'] = array();
  
  if(isset($_GET['search'])){
    echo "&lt;table border=1&gt;";
    echo "&lt;th&gt;Product Image&lt;/th&gt;";
    echo "&lt;th&gt;Product Name&lt;/th&gt;";
    echo "&lt;th&gt;Price&lt;/th&gt;";
  
    foreach($xml-&gt;categories-&gt;category-&gt;items-&gt;product as $product){
      $imageURL = $product-&gt;images-&gt;image[0]-&gt;sourceURL;
      $id = $product['id'];
      echo "&lt;tr&gt;";
      echo "&lt;td&gt;&lt;a href= 'buy.php?buy=".$id."'&gt;&lt;img src=".$imageURL."&gt;&lt;/img&gt;&lt;/a&gt;&lt;/td&gt;";
      echo "&lt;td&gt;".$product-&gt;name."&lt;/td&gt;";
      echo "&lt;td&gt;".'$'.$product-&gt;minPrice."&lt;/td&gt;";
    }
  }
  
  if(isset($_GET['buy'])){
    $product_id = $_GET['buy'];
    if(isset($_SESSION['cart'])){
      array_push($_SESSION['cart'],$product_id);
    }
  }
  
  print_r ($_SESSION);
  ?&gt;
  </code></pre>
  
  <p>What the first if statement does is that it gets the search word, and gets the closest results. It then displays an image, name, and price. When you click on the image, there is a href and it is supposed to be added to the shopping cart. That is where the second if statement comes into play. If the image has been clicked I want to get the id and store it in session. It stores the id of the product when clicked upon, but when I go back to add another item the previous id is replaced with the new id. Could anybody explain to me where I went wrong? Any help would be greatly appreciated.</p>