title: How to create an online shopping cart in php
body: <p>I am creating a shopping cart which has four buttons continue shopping, clear cart, update cart and place order I am facing a problem with the sessions like when I  press the clear cart button it shows the</p>
  
  <pre><code> undefined index cart 
  </code></pre>
  
  <p>but the cart comes from the session and when i press the clear cart button </p>
  
  <pre><code>it unsets the session of cart 
  </code></pre>
  
  <p>so where i am doing it wrong ? can anyone help ?
  here is my code</p>
  
  <pre><code>&lt;?php
      include("includes/db.php");
      include("includes/functions.php");
  
      if($_REQUEST['command']=='delete' &amp;&amp; $_REQUEST['pid']&gt;0){
          remove_product($_REQUEST['pid']);
      }
      else if($_REQUEST['command']=='clear'){
          unset($_SESSION['cart']);
      }
      else if($_REQUEST['command']=='update'){
          $max=count($_SESSION['cart']);
          for($i=0;$i&lt;$max;$i++){
              $pid=$_SESSION['cart'][$i]['productid'];
              $q=intval($_REQUEST['product'.$pid]);
              if($q&gt;0 &amp;&amp; $q&lt;=999){
                  $_SESSION['cart'][$i]['qty']=$q;
  
              }
              else{
                  $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
              }
          }
      }
  
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Shopping Cart&lt;/title&gt;
  &lt;script language="javascript"&gt;
      function del(pid){
          if(confirm('Do you really mean to delete this item')){
              document.form1.pid.value=pid;
              document.form1.command.value='delete';
              document.form1.submit();
          }
      }
      function clear_cart(){
          if(confirm('This will empty your shopping cart, continue?')){
              document.form1.command.value='clear';
              document.form1.submit();
          }
      }
      function update_cart(){
          document.form1.command.value='update';
          document.form1.submit();
      }
  
  
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form name="form1" method="post"&gt;
  &lt;input type="hidden" name="pid" /&gt;
  &lt;input type="hidden" name="command" /&gt;
      &lt;div style="margin:0px auto; width:600px;" &gt;
      &lt;div style="padding-bottom:10px"&gt;
          &lt;h1 align="center"&gt;Your Shopping Cart&lt;/h1&gt;
      &lt;input type="button" value="Continue Shopping" onclick="window.location='products.php'" /&gt;
      &lt;/div&gt;
          &lt;div style="color:#F00"&gt;&lt;?php echo'' ;?&gt;&lt;/div&gt;
          &lt;table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%"&gt;
          &lt;?php
              if(is_array($_SESSION['cart'])){
                  echo '&lt;tr bgcolor="#FFFFFF" style="font-weight:bold"&gt;&lt;td&gt;Serial&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Price&lt;/td&gt;&lt;td&gt;Qty&lt;/td&gt;&lt;td&gt;Amount&lt;/td&gt;&lt;td&gt;Options&lt;/td&gt;&lt;/tr&gt;';
                  $max=count($_SESSION['cart']);
                  for($i=0;$i&lt;$max;$i++){
                      $pid=$_SESSION['cart'][$i]['productid'];
                      $q=$_SESSION['cart'][$i]['qty'];
                      $pname=get_product_name($pid);
                      if($q==0) continue;
              ?&gt;
                      &lt;tr bgcolor="#FFFFFF"&gt;&lt;td&gt;&lt;?php echo $i+1?&gt;&lt;/td&gt;&lt;td&gt;&lt;?php echo $pname?&gt;&lt;/td&gt;
                      &lt;td&gt;$ &lt;?php echo get_price($pid)?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="product&lt;?php echo $pid?&gt;" value="&lt;?php echo $q?&gt;" maxlength="3" size="2" /&gt;&lt;/td&gt;                    
                      &lt;td&gt;$ &lt;?php echo get_price($pid)*$q?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;a href="javascript:del(&lt;?php echo $pid?&gt;)"&gt;Remove&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;?php                   
                  }
              ?&gt;
                  &lt;tr&gt;&lt;td&gt;&lt;b&gt;Order Total: $&lt;?php echo get_order_total()?&gt;&lt;/b&gt;&lt;/td&gt;&lt;td colspan="5" align="right"&gt;&lt;input type="button" value="Clear Cart" onclick="clear_cart()"&gt;&lt;input type="button" value="Update Cart" onclick="update_cart()"&gt;&lt;input type="button" value="Place Order" onclick="window.location='billing.php'"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;?php
              }
              else{
                  echo "&lt;tr bgColor='#FFFFFF'&gt;&lt;td&gt;There are no items in your shopping cart!&lt;/td&gt;";
              }
          ?&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>