title: Shopping cart save data to database php mysql
body: <p>I am currently working on saving a data to database using php mysql in shopping cart</p>
  
  <p>cart.php</p>
  
  <pre><code>&lt;?php
          foreach($_SESSION['cart'] as $product_id =&gt; $quantity) {    
  
  
              $sql = sprintf("SELECT prod_name, prod_code, prod_desc, prod_category, prod_price FROM product WHERE prod_id = %d;",
                              $product_id); 
  
              $result = mysql_query($sql);
  
              //Only display the row if there is a product (though there should always be as we have already checked)
              if(mysql_num_rows($result) &gt; 0) {
  
                  list($name, $code, $description, $category, $price) = mysql_fetch_row($result);
  
                  $line_cost = $price * $quantity;        //work out the line cost
                  $total = $total + $line_cost;           //add to the total cost
  
                  echo "&lt;tr&gt;";
  
                      echo "&lt;td align=\"center\"&gt;$name&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;$code&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;$description&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;$category&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;$quantity&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;&lt;a href=\"index.php?page=inso94&amp;action=add&amp;id=$product_id\" class=\"btn btn-info btn-sm btn-fill pull-right\"&gt;Add quantity&lt;/a&gt;&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;&lt;a href=\"index.php?page=inso94&amp;action=remove&amp;id=$product_id\" class=\"btn btn-warning btn-sm btn-fill pull-right\"&gt;Reduce&lt;/a&gt;&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;$price&lt;/td&gt;";
                      echo "&lt;td align=\"center\"&gt;$line_cost&lt;/td&gt;";
  
                  echo "&lt;/tr&gt;";
  
              }
  
          }
  
  
          echo "&lt;tr&gt;";
              echo "&lt;td colspan=\"0\" align=\"left\"&gt; &lt;a href=\"index.php?page=inso94&amp;action=empty\" class=\"btn btn-danger btn-sm btn-fill pull-right\"&gt;Empty Cart&lt;/a&gt;&lt;/td&gt;";
              echo "&lt;td colspan=\"7\" align=\"right\"&gt;Total&lt;/td&gt;";
              echo "&lt;td align=\"right\"&gt;&lt;b&gt;$total&lt;/b&gt;&lt;/td&gt;";
          echo "&lt;/tr&gt;";
          echo "&lt;tr&gt;";
              echo "&lt;td align=\"left\" colspan=\"9\"&gt;Note: If quantity becomes &lt;b&gt;'0'&lt;/b&gt; it will automatically remove.&lt;/td&gt;";
          echo "&lt;/tr&gt;";
      echo "&lt;/table&gt;";
      ?&gt;
          *&lt;?php
              if (isset($_POST['submit'])){
                 $orderid=mysql_insert_id();
  
                  $max=count($_SESSION['cart']);
                  for($i=0;$i&lt;$max;$i++){
                      $pid=$_SESSION['cart'][$i]['productid'];
                      $q=$_SESSION['cart'][$i]['quantity'];
                      $total;
                      mysql_query("insert into order_detail values ($orderid,$pid,$q,$total)");
              }
          }
          ?&gt;
          &lt;form action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;" method="POST"&gt;    
              &lt;button type="submit" name="submit" class="btn btn-info btn-sm btn-fill pull-right" id="checkout"&gt;Buy Now&lt;/button&gt;
          &lt;/form&gt;*
  </code></pre>
  
  <p>I want to save the data in to my database using a form .please help me, this is my last problem that i want to solve.</p>