title: PHP and SQL Shopping Cart is Not Adding Products to Shopping Cart
body: <p>I am trying to make a simple MSSQL and PHP shopping cart for my website. The page <code>index.php</code> correctly retrieves the products from my MSSQL <code>products</code> table and displays them. For each product displayed there is an <code>Add to Cart</code> button. The button should use <code>$_SESSION</code> to store the added product into the shopping cart via the <code>cart_update.php</code> page. The products added to the shopping cart should be displayed at the bottom of the index.php page. Currently, when the <code>Add to Cart</code> button is hit, no products are added to the shopping cart. I am unsure why this is happening.</p>
  
  <p>Here is my <code>index.php</code> page code:</p>
  
  <pre><code>&lt;?php
  session_start();
  include_once("config.php");
  ?&gt;
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div class="products"&gt;
  &lt;?php
  //current URL of the Page. cart_update.php redirects back to this URL
  $current_url = base64_encode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
      $query = "SELECT * FROM products ORDER BY id ASC";
      $results = mssql_query($query, $mysqli);
      if ($results) { 
          //output results from database
          while($obj = mssql_fetch_object($results))
          {
  
              echo '&lt;div class="product"&gt;'; 
              echo '&lt;form method="post" action="cart_update.php"&gt;';
              echo '&lt;div class="product-thumb"&gt;&lt;img src="images/'.$obj-&gt;product_img_name.'"&gt;&lt;/div&gt;';
              echo '&lt;div class="product-content"&gt;&lt;h3&gt;'.$obj-&gt;product_name.'&lt;/h3&gt;';
              echo '&lt;div class="product-desc"&gt;'.$obj-&gt;product_desc.'&lt;/div&gt;';
              echo '&lt;div class="product-info"&gt;Price '.$currency.$obj-&gt;price.' &lt;button class="add_to_cart"&gt;Add To Cart&lt;/button&gt;&lt;/div&gt;';
              echo '&lt;/div&gt;';
              echo '&lt;input type="hidden" name="product_code" value="'.$obj-&gt;product_code.'" /&gt;';
              echo '&lt;input type="hidden" name="type" value="add" /&gt;';
              echo '&lt;input type="hidden" name="return_url" value="'.$current_url.'" /&gt;';
              echo '&lt;/form&gt;';
              echo '&lt;/div&gt;';
          }
  
  }
  ?&gt;
  &lt;/div&gt;
  &lt;div class="shopping-cart"&gt;
  &lt;h2&gt;Your Shopping Cart&lt;/h2&gt;
  &lt;?php
  if(isset($_SESSION["products"]))
  {
      $total = 0;
      echo '&lt;ol&gt;';
      foreach ($_SESSION["products"] as $cart_itm)
      {
          echo '&lt;li class="cart-itm"&gt;';
          echo '&lt;span class="remove-itm"&gt;&lt;a href="cart_update.php?removep='.$cart_itm["code"].'&amp;return_url='.$current_url.'"&gt;&amp;times;&lt;/a&gt;&lt;/span&gt;';
          echo '&lt;h3&gt;'.$cart_itm["name"].'&lt;/h3&gt;';
          echo '&lt;div class="p-code"&gt;P code : '.$cart_itm["code"].'&lt;/div&gt;';
          echo '&lt;div class="p-qty"&gt;Qty : '.$cart_itm["qty"].'&lt;/div&gt;';
          echo '&lt;div class="p-price"&gt;Price :'.$currency.$cart_itm["price"].'&lt;/div&gt;';
          echo '&lt;/li&gt;';
          $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
          $total = ($total + $subtotal);
      }
      echo '&lt;/ol&gt;';
      echo '&lt;span class="check-out-txt"&gt;&lt;strong&gt;Total : '.$currency.$total.'&lt;/strong&gt; &lt;a href="view_cart.php"&gt;Check-out!&lt;/a&gt;&lt;/span&gt;';
      echo '&lt;span class="empty-cart"&gt;&lt;a href="cart_update.php?emptycart=1&amp;return_url='.$current_url.'"&gt;Empty Cart&lt;/a&gt;&lt;/span&gt;';
  }else{
      echo 'Your Cart is empty';
  }
  ?&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is my <code>cart_update.php</code> page code:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;?php
  session_start(); //start session
  include_once("config.php"); //include config file
  
  //empty cart by distroying current session
  if(isset($_GET["emptycart"]) &amp;&amp; $_GET["emptycart"]==1)
  {
      $return_url = base64_decode($_GET["return_url"]); //return url
      session_destroy();
      header('Location:'.$return_url);
  }
  
  //add item in shopping cart
  if(isset($_POST["type"]) &amp;&amp; $_POST["type"]=='add')
  {
      $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
      $product_qty    = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
      $return_url     = base64_decode($_POST["return_url"]); //return url
  
      //limit quantity for single product
      if($product_qty &gt; 10){
          die('&lt;div align="center"&gt;This demo does not allowed more than 10 quantity!&lt;br /&gt;&lt;a href="http://sanwebe.com/assets/paypal-shopping-cart-integration/"&gt;Back To Products&lt;/a&gt;.&lt;/div&gt;');
      }
      $query = "SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1";
      //MySqli query - get details of item from db using product code
      $results = mssql_query($query, $mysqli);
      $obj = mssql_fetch_object($results);
  
      if ($results) { //we have the product info 
  
          //prepare array for the session variable
          $new_product = array(array('name'=&gt;$obj-&gt;product_name, 'code'=&gt;$product_code, 'qty'=&gt;$product_qty, 'price'=&gt;$obj-&gt;price));
  
          if(isset($_SESSION["products"])) //if we have the session
          {
              $found = false; //set found item to false
  
              foreach ($_SESSION["products"] as $cart_itm) //loop through session array
              {
                  if($cart_itm["code"] == $product_code){ //the item exist in array
  
                      $product[] = array('name'=&gt;$cart_itm["name"], 'code'=&gt;$cart_itm["code"], 'qty'=&gt;$product_qty, 'price'=&gt;$cart_itm["price"]);
                      $found = true;
                  }else{
                      //item doesn't exist in the list, just retrive old info and prepare array for session var
                      $product[] = array('name'=&gt;$cart_itm["name"], 'code'=&gt;$cart_itm["code"], 'qty'=&gt;$cart_itm["qty"], 'price'=&gt;$cart_itm["price"]);
                  }
              }
  
              if($found == false) //we didn't find item in array
              {
                  //add new user item in array
                  $_SESSION["products"] = array_merge($product, $new_product);
              }else{
                  //found user item in array list, and increased the quantity
                  $_SESSION["products"] = $product;
              }
  
          }else{
              //create a new session var if does not exist
              $_SESSION["products"] = $new_product;
          }
  
      }
  
      //redirect back to original page
      header('Location:'.$return_url);
  }
  
  //remove item from shopping cart
  if(isset($_GET["removep"]) &amp;&amp; isset($_GET["return_url"]) &amp;&amp; isset($_SESSION["products"]))
  {
      $product_code   = $_GET["removep"]; //get the product code to remove
      $return_url     = base64_decode($_GET["return_url"]); //get return url
  
  
      foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
      {
          if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
              $product[] = array('name'=&gt;$cart_itm["name"], 'code'=&gt;$cart_itm["code"], 'qty'=&gt;$cart_itm["qty"], 'price'=&gt;$cart_itm["price"]);
          }
  
          //create a new product list for cart
          $_SESSION["products"] = $product;
      }
  
      //redirect back to original page
      header('Location:'.$return_url);
  }
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thank you for any help. All help is greatly appreciated.</p>