When I enter the product at add_cart.php it say already enter. But when I want to view it at view_cart.php it says Your empty cart is empty.Why?Can someone please help me!
add_cart.php script
<?php
// This page adds prints to the shopping cart.
if (is_numeric ($_GET['pid'])) {
// Check for a print ID.
// Set the page title and include the HTML header.
$page_title = 'Add to Cart';
include_once ('includes/header_customer_two.html');
// Check if the cart already contains one of these prints.
if (isset ($_SESSION['cart'][$pid])) {
$qty = $_SESSION['cart'][$pid] + 1;
} else {
$qty = 1;
}
// Add to the cart session variable.
$_SESSION['cart'][$pid] = $qty;
// Display a message.
echo '<p>The print has been added to your shopping cart.</p>';
} else { // Redirect
header ("Location: [url]http://[/url]" . $SERVER['HTTP_HOST'] . dirname($SERVER['PHP_SELF']) . "/index.php");
exit();
}
?>
view_cart.php script
<?php
// This page displays the contents of the shopping cart.
// Set the page title and include the HTML header.
$page_title = 'View Your Shopping Cart';
include_once ('includes/header_customer_two.html');
// Check if the form has been submitted (to update the cart)
if (isset ($POST['submit'])) {
foreach ($POST['qty'] as $key => $value) {
if (($value == 0) AND (is_numeric ($value))) {
unset ($SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) ) {
$SESSION['cart'][$key] = $value;
}
}
}
// Check if the shopping cart is empty.
$empty = TRUE;
if (isset ($SESSION['cart'])) {
foreach ($SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}
}
// Display the cart if it's not empty.
if (!$empty) {
require_once ('./mysql_connect.php'); // Connect to the database.
// Retrieve all of the information for the prints in the cart.
$query = 'SELECT * FROM category, product WHERE category.category_id = product.category_id AND product.product_id IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY category.last_category ASC';
$result = mysql_query ($query);
// Create a table and a form.
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>Author</b></td>
<td align="left" width="30%"><b>Book Title</b></td>
<td align="right" width="10%"><b>Price</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="10%"><b>Total Price</b></td>
</tr>
<form action="view_cart.php" method="post">
';
// Print each item.
$total = 0; // Total cost of the order.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
// Calculate the total and subtotals.
$subtotal = $_SESSION['cart'][$row['product_id']] * $row['price'];
$total += $subtotal;
// Print the row.
echo " <tr>
<td align=\"left\">{$row['first_category']} {$row['middle_category']} {$row['last_category']}</td>
<td align=\"left\">{$row['product_name']}</td>
<td align=\"right\">\${$row['price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2). "</td>
</tr>\n";
} // End of the WHILE loop.
// Print the footer and close the table and the form.
echo ' <tr>
<td colspan="4" align="right"><b>Total : <b></td>
<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table><div align="center"><inputr type="submit" name="submit" value="Update My Cart" /></form><br /><br /><a href="checkout.php"><font size="+3">Checkout</font></a></div>';
mysql_close(); // Close the database connection.
} else {
echo '<p>Your cart is currently empty.</p>';
}
?>
Let me know ya which part to edit.I really appreciate ur help.