I dont know where the bug is even after dwelling on the code for hours.
Problems it gives:
1. when i click the link add, remove and delete, simply display the last echo statement You cart is empty.
<?php
include 'includes/connect.php';
session_start();
mysql_connect('localhost','root','')or die('Could not connect to server');
$db = mysql_select_db('book_sc') or die('Could not connect database');
echo "<h1> Shopping cart </h1><br />";
if (isset($_GET['id']))
$id = $_GET['id'];
else
$id='1';
if (isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch($action)
{
case 'add':
if (isset($_SESSION['cart'][$id]))
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case 'remove':
if (isset($_SESSION['cart'][$id]))
{
$_SESSION['cart'][$id]--;
if ($_SESSION['cart'][$id]==0)
unset($_SESSION['cart'][$id]);
}
break;
case 'empty':
unset($_SESSION['cart']);
break;
}
if(isset($SESSION['cart']))
{
echo "<table border=1 cellpadding=0 cellspacing=0 width='500'>";
$totalcost = '0';
foreach( $SESSION['cart'] as $id => $x)
{
$result = mysql_query("SELECT title,price FROM books WHERE id=$id")or die(mysql_error());
$myrow = mysql_fetch_array($result);
$title =$myrow['title'];
$price =$myrow['price'];
$linecost = $price * $x;
$totalcost = $totalcost + $linecost;
echo "<tr>";
echo "<td align=left>$title</td>";
echo "<td align=right>$x<a href='displaycart.php?id='.$id.'&action=remove'>Reduce</a>
<a href='displaycart.php?id='.$id.'&action=add'>Add</a>
<a href='displaycart.php?id='.$id.'&action=empty'>Empty</a></td>";
echo "<td align=right>= Rs. $linecost</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td align=right> <br>Total</td>";
echo "<td align=right><b> Rs. $totalcost</b></td>";
echo "</tr>";
echo "</table>";
}
else
echo "The cart is empty";
?>