title: PHP Shopping Cart Add Items
body: <p>I'm currently working on a shopping cart using PHP, and I'm trying to figure out how to add items to the cart itself using the code I have written. The items from my database are being displayed correctly, but only the last array under $item is being added to the cart. The following displays the items.</p>
  
  <pre><code>$result = mysqli_query($cxn,$sql) or die("&lt;p class='error'&gt;Couldn't connect to server.&lt;/p&gt;");
  while($row = mysqli_fetch_assoc($result))
  {
      $product[] = $row;
  }
  foreach($product as $item)
  {
      echo "&lt;div class='product'&gt;&lt;form method='post'&gt;&lt;div class='img_spacer'&gt;&lt;div class='image'&gt;";
      include "images.inc";
      echo "&lt;/div&gt;&lt;/div&gt;&lt;div class='name'&gt;&lt;h2&gt;".$item['product']."&lt;/h2&gt;&lt;/div&gt;";
      echo "&lt;div class='description'&gt;&lt;p&gt;".$item['description']."&lt;/p&gt;&lt;/div&gt;";
      echo "&lt;div class='price'&gt;&lt;p&gt;".$item['price']."&lt;/p&gt;&lt;/div&gt;";
      echo "&lt;div class='add_cart'&gt;&lt;input type='hidden' name='add' value='yes'&gt;
      &lt;input type='submit' name='add_cart' value='Add to Cart'&gt;
      &lt;/div&gt;&lt;/form&gt;&lt;/div&gt;";
  }
  </code></pre>
  
  <p>The following code is for the shopping cart itself. I have it currently set to print_r the sent variables so I can see what information is being posted.</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['add']) and $_POST['add'] == 'yes')
  {
      $selected = "select product_ID, product, price from product where product_ID='".$item['product_ID']."'";
      $result2 = mysqli_query($cxn,$selected);
      while($row2 = mysqli_fetch_assoc($result2))
      {
          print_r($row2);
      }
  }
  ?&gt;
  </code></pre>
  
  <p>I also tried adding the $item['product_ID'] variable to make the 'add' input unique, using </p>
  
  <pre><code>&lt;input type='hidden' name='".$item['product_ID']."_add' value='yes'&gt;
  </code></pre>
  
  <p>but I couldn't figure out how to add another variable to the $_POST array. I should also mention that I'm using sessions for this project, and I'm not quite sure how to add their shopping cart to the $_SESSION variable. How can I fix this?</p>