shopping cart Session php

amoosh picture amoosh · Apr 12, 2013 · Viewed 18.4k times · Source
    <?php

session_start(); 
print '<h1>Your Shopping Cart:</h1>';
print '<h4>The Product ID: '.$_SESSION['id'].' </h4>';
print '<h4>The Quantity: '.$_SESSION['quantity'].' </h4>';

$dbc = mysql_connect('127.0.0.1', 'root', ''); 
mysql_select_db('product', $dbc);

$query = "SELECT * FROM prod WHERE id='".$_SESSION['id']."'";



if ($r = mysql_query($query, $dbc)) {

while ($row = mysql_fetch_array($r)) { 
print "<p><b>Product Name: {$row['name']}<br />
             The Price: {$row['price']}<br />
             Shipping Cost: {$row['shipping']}<br />



 </p><hr />\n";
 }}
?>

This code for shopping cart with Session but the problem is that it keeps only one product.

For example, if you purchase a product A and in Cart product B delete B then add A product Please help me I want to add more than one product and printed on the screen ##

Answer

Marc B picture Marc B · Apr 12, 2013

Add another level to your cart:

$_SESSION['cart'][$productID]['quantity'];
                 ^^^^^^^^^^^^^

so you keep per-product data. Right now you're using a single level that you're continually overwriting as new products are added.