I need to store data from html table to mysql database using php : I am new to stackoverflow. I am doing a project with html, css, php and mysql. The project is online vegetable shop which is nearly an ecommerce site. I need to store data from html table(my cart) to mysql database using php. The table looks like this:
http://i.imgur.com/OngFBuP.png

the code for "cart" is:

    '<?php
        include("session.php");
        include_once("config.php");
        ?>
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>View shopping cart</title>

        <link href="style/style.css" rel="stylesheet" type="text/css"></head>
        <body>
        <h1 align="center">View Cart</h1>
        <div class="cart-view-table-back">
        <form method="post" action="buy.php">
        <table id="myTable" width="100%"  cellpadding="6" cellspacing="0">                        <thead><tr><th>Sl_no</th><th>Username</th><th>Quantity</th><th>Name</th>        <th>Price</th><th>Total</th><th>Remove</th></tr></thead>
          <tbody>
         	<?php
        	if(isset($_SESSION["cart_products"]))
            {
		        $total = 0;
        		$fl=1;
        		$b = 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"];
        			$subtotal = ($product_price * $product_qty); 
        			
        		   	$bg_color = ($b++%2==1) ? 'odd' : 'even'; 
		    
			
        			echo '<tr class="'.$bg_color.'">';
		        	echo '<td>'.$fl.'</td>';
        			echo '<td>'.$user.'</td>';
		        	echo '<td><input type="text" size="2" maxlength="2"
        name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
        			echo '<td>'.$product_name.'</td>';
		        	echo '<td>'.$product_price.'</td>';
        			echo '<td>'.$subtotal.'</td>';
		        	echo '<td><input type="checkbox" name="remove_code[]"
        value="'.$product_code.'" /></td>';
                    echo '</tr>';
		        	$fl++;
        			$total = ($total + $subtotal); 
                }
        		echo$fl;
        		$grand_total = $total; 
		
        }
		
			
            ?>
    
    
          </tbody>
        </table>
        <button name="btn1" type="submit">buy</button>
        <input type="hidden" name="return_url" value="<?php 
        $current_url =
        urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        echo $current_url; ?>" />
        </form>

        </div>

      </body>
        </html>'

And I am using a php page(buy.php) to store all the data to database. The code for "buy.php" is:

        '<?php

        include('session.php');
        $con=new mysqli("localhost","root","","test");
        $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"];
			$subtotal = ($product_price * $product_qty);
			$total = ($total + $subtotal);
		}
        		$sql="INSERT INTO cart (Uname,TotalCost,veg_id,quantity) 
    VALUES('$user','$total','$product_code','$product_qty')";
		if($con->query($sql)==true){
			echo"data inserted successfully";
		}
		echo"<br><br><a href='index_2.php'> Return to Your Dashboard</a>
   	    <a href='buy_veg.php'> Buy More Items</a>";

		?>'
        

The problem is this "buy.php" page is actually storing the last record to the database. Image link:
http://i.imgur.com/ZaCnvjI.png

I want to store all the cart items to the database. I need to submit my project tomorrow. Please help me as fast as possible.

Thank You .