title: Items not showing up in my view cart page
body: <p>I am trying to create an e-commerce website for the school project that fetches information from a database I created. Unfortunately this was the code we were partially given and I can't get the items added to the shopping cart to show in the check out page.</p>
  
  <p>Here is the code for the main page </p>
  
  <pre><code>&lt;div class="products"&gt;
  &lt;?php
  session_start();
  include_once("config.php");
  
  $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  
  $results = $mysqli-&gt;query("SELECT product_code, product_name, product_desc, price FROM products");
  if($results){
  $products_item = '&lt;ul class="products"&gt;';
  //fetch results set as object and output HTML
  while($obj = $results-&gt;fetch_object())
  {
  $products_item .= &lt;&lt;&lt;EOT
      &lt;li class="product"&gt;
      &lt;form method="post" action="cart_update.php"&gt;
      &lt;div class="product-content"&gt;&lt;h3&gt;{$obj-&gt;product_name}&lt;/h3&gt;
      &lt;div class="product-desc"&gt;{$obj-&gt;product_desc}&lt;/div&gt;
      &lt;div class="product-info"&gt;
      Price {$currency}{$obj-&gt;price}
  
      &lt;fieldset&gt;
  
      &lt;label&gt;
          &lt;span&gt;Quantity&lt;/span&gt;
          &lt;input type="text" size="2" maxlength="2" name="product_qty" value="1" /&gt;
      &lt;/label&gt;
  
      &lt;/fieldset&gt;
      &lt;input type="hidden" name="product_code" value="{$obj-&gt;product_code}" /&gt;
      &lt;input type="hidden" name="type" value="add" /&gt;
      &lt;input type="hidden" name="return_url" value="{$current_url}" /&gt;
      &lt;div align="center"&gt;&lt;button type="submit" class="add_to_cart"&gt;Add&lt;/button&gt;&lt;/div&gt;
      &lt;/div&gt;&lt;/div&gt;
      &lt;/form&gt;
      &lt;/li&gt;
  EOT;
  }
  $products_item .= '&lt;/ul&gt;';
  echo $products_item;
  }
  ?&gt;
  &lt;/div&gt;
  
  &lt;div class="shopping-cart"&gt;
  &lt;?php
  if(isset($_SESSION["cart_products"]) &amp;&amp; count($_SESSION["cart_products"])&gt;0)
  {
      echo '&lt;div class="cart-view-table-front" id="view-cart"&gt;';
      echo '&lt;h3&gt;Your Shopping Cart&lt;/h3&gt;';
      echo '&lt;form method="post" action="cart_update.php"&gt;';
      echo '&lt;table width="100%"  cellpadding="6" cellspacing="0"&gt;';
      echo '&lt;tbody&gt;';
  
      $total =0;
      foreach ($_SESSION["cart_products"] as $cart_itm)
      {
          $product_name = $cart_itm["product_name"];
          $product_qty = $cart_itm["product_qty"];
          $product_price = $cart_itm["product_price"];
          $product_code = $cart_itm["product_code"];
          echo '&lt;td&gt;Qty &lt;input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /&gt;&lt;/td&gt;';
          echo '&lt;td&gt;'.$product_name.'&lt;/td&gt;';
          echo '&lt;td&gt;&lt;input type="checkbox" name="remove_code[]" value="'.$product_code.'" /&gt; Remove&lt;/td&gt;';
          echo '&lt;/tr&gt;';
          $subtotal = ($product_price * $product_qty);
          $total = ($total + $subtotal);
      }
      echo '&lt;td colspan="4"&gt;';
      echo '&lt;button type="submit"&gt;Update&lt;/button&gt;&lt;a href="view_cart.php" class="button"&gt;Checkout&lt;/a&gt;';
      echo '&lt;/td&gt;';
      echo '&lt;/tbody&gt;';
      echo '&lt;/table&gt;';
  
      $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
      echo '&lt;input type="hidden" name="return_url" value="'.$current_url.'" /&gt;';
      echo '&lt;/form&gt;';
      echo '&lt;/div&gt;';
  
  }
  ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This is the update cart php file</p>
  
  <pre><code>&lt;?php
  session_start(); //start session
  include_once("config.php"); //include config file
  
  //add product to session or create new one
  if(isset($_POST["type"]) &amp;&amp; $_POST["type"]=='add' &amp;&amp; $_POST["product_qty"]&gt;0)
  {
      foreach($_POST as $key =&gt; $value){ //add all post vars to new_product array
          $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
      }
      //remove unecessary vars
      unset($new_product['type']);
      unset($new_product['return_url']);
  
      //we need to get product name and price from database.
      $statement = $mysqli-&gt;prepare("SELECT product_name, price FROM products");
      $statement-&gt;bind_param('s', $new_product['product_code']);
      $statement-&gt;execute();
      $statement-&gt;bind_result($product_name, $price);
  
      while($statement-&gt;fetch()){
  
          //fetch product name, price from db and add to new_product array
          $new_product["product_name"] = $product_name;
          $new_product["product_price"] = $price;
  
          if(isset($_SESSION["cart_products"])){  //if session var already exist
              if(isset($_SESSION["cart_products"][$new_product['product_code']])) //check item exist in products array
              {
                  unset($_SESSION["cart_products"][$new_product['product_code']]); //unset old array item
              }
          }
          $_SESSION["cart_products"][$new_product['product_code']] = $new_product; //update or create product session with new item
      }
  }
  
  
  //update or remove items
  if(isset($_POST["product_qty"]) || isset($_POST["remove_code"]))
  {
      //update item quantity in product session
      if(isset($_POST["product_qty"]) &amp;&amp; is_array($_POST["product_qty"])){
          foreach($_POST["product_qty"] as $key =&gt; $value){
              if(is_numeric($value)){
                  $_SESSION["cart_products"][$key]["product_qty"] = $value;
              }
          }
      }
      //remove an item from product session
      if(isset($_POST["remove_code"]) &amp;&amp; is_array($_POST["remove_code"])){
          foreach($_POST["remove_code"] as $key){
              unset($_SESSION["cart_products"][$key]);
          }
      }
  }
  
  //back to return url
  $return_url = (isset($_POST["return_url"]))?urldecode($_POST["return_url"]):''; //return url
  header('Location:'.$return_url);
  </code></pre>
  
  <p>And finally, the checkout page php file</p>
  
  <pre><code>&lt;div class="cart-view-table-back"&gt;
  &lt;form method="post" action="cart_update.php"&gt;
  &lt;table width="100%"  cellpadding="6" cellspacing="0"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Quantity&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;th&gt;Total&lt;/th&gt;&lt;th&gt;Remove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
    &lt;body&gt;
      &lt;?php
  
    $total = 0; //set initial total value
      if(isset($_SESSION["cart_products"])) //check session var
      {
  
          foreach ($_SESSION["cart_products"] as $cart_itm)
          {
              //set variables to use in content below
              $product_name = $cart_itm["product_name"];
              $product_qty = $cart_itm["product_qty"];
              $product_price = $cart_itm["product_price"];
              $product_code = $cart_itm["product_code"];
              $subtotal = ($product_price * $product_qty);
  
              echo '&lt;td&gt;&lt;input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /&gt;&lt;/td&gt;';
              echo '&lt;td&gt;'.$product_name.'&lt;/td&gt;';
              echo '&lt;td&gt;'.$currency.$product_price.'&lt;/td&gt;';
              echo '&lt;td&gt;'.$currency.$subtotal.'&lt;/td&gt;';
              echo '&lt;td&gt;&lt;input type="checkbox" name="remove_code[]" value="'.$product_code.'" /&gt;&lt;/td&gt;';
              echo '&lt;/tr&gt;';
              $total = ($total + $subtotal); //add subtotal to total var
          }
          }
      ?&gt;
      &lt;tr&gt;&lt;td colspan="5"&gt;&lt;span style="float:right;text-align: right;"&gt;Amount Payable : &lt;?php echo "$".sprintf("%01.2f", $total);?&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td colspan="5"&gt;&lt;a href="index.php" class="button"&gt;Add More Items&lt;/a&gt;&lt;button type="submit"&gt;Update&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;/body&gt;
  &lt;/table&gt;
  &lt;input type="hidden" name="return_url" value="&lt;?php
  $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  echo $current_url; ?&gt;" /&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  
  
  </code></pre>