Shopping Cart update problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vittelite
Forum Newbie
Posts: 12
Joined: Tue Mar 08, 2005 9:23 am

Shopping Cart update problem...

Post by vittelite »

Hi guys,
I'm having a small problem updating shopping cart contents. I can add contents fine and when I load up the shopping cart I am given options to change the quantity. However whenever I hit the submit button my shopping cart values set to 0 and I cannot do anything to them again without deleting all the session variables again. If anyone can help I would really appreciate it, thank you!! Below are my scripts for adding and viewing the shopping cart.

To add to cart:

Code: Select all

include ('header.html');
include_once ('mysql_connect.php');


// Add a book to the shopping cart

if (is_numeric ($_GET['bid'])) { // Checks whether there is a valid book ID.
$bid = $_GET['bid'];

// Make sure the cart does not already contain a book.
if (isset ($_SESSION['cart'][$bid])) {
$qty = $_SESSION['cart'][$bid] + 1;
} else {
$qty = 1;
}

// Add book to the cart session variable.
$_SESSION['cart'][$bid] = $qty;

echo '<p align="center"><b>You have successfully added the book to your shopping cart!</b>
<br><br> To view your shopping cart <a href="shopping_cart1.php">click here</a>
otherwise <a href="search.php">search</a> for another book.</p>';

echo '<hr>';
include ('footer.html');
} else { // Redirect user
header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
exit();
}

To view the cart:

Code: Select all

include ('header.html');


// Check if form has been submitted to update 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 cart is empty
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}
}
if (!$empty) {
include ('mysql_connect.php');
// Retrieve the book information currently in the cart.
$query = 'SELECT * FROM books, course WHERE books.isbn = course.isbn AND course.book_id IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}

$query = substr ($query, 0, -1) . ') ORDER BY books.author ASC';
$result = mysql_query ($query);

// Create table and form.
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
	<td align="left" width="5%"><b>Image</b></td>
	<td align="left" width="30%"><b>Title</b></td>
	<td align="left" width="30%"><b>Author</b></td>
	<td align="right" width="10%"><b>Price</b></td>
	<td align="center" width="5%"><b>Quantity</b></td>
	<td align="right" width="10%"><b>Subtotal</b></td>
</tr>

<form action="shopping_cart.php" method="post">
';

// Print items.
$total = 0; // Total Cost of Order.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

// Calculate Subtotal + total.
$subtotal = $_SESSION['cart'][$row['book_id']] * $row['price'];
$subtotal1 += $subtotal;
$vat = 0.175 * $subtotal1;
$total = $subtotal1 + $vat;


	// Print row.
	echo " <tr>
	<td>
	<img width=60px height=81px src=\"images/books/./{$row['image_name']}\" $image[3] alt=\"{$row['image_name']}\" />
	</td>
	<td align=\"left\">{$row['name']}</td>
	<td align=\"left\">{$row['author']}</td>
	<td align=\"right\">£{$row['price']}</td>
	<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{row['book_id']}]\"
value=\"{$_SESSION['cart'][$row['book_id']]}\" /></td>
<td align=\"right\">£" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // end of while loop.
echo ' <tr>
<td colspan="5" align="right"><b>Subtotal:<b></td>
<td align="right">£' . number_format ($subtotal1, 2) . '</td>
</tr>
<tr>
<td colspan="5" align="right"><b>VAT:<b></td>
<td align="right">£' . number_format ($vat, 2) . '</td>
</tr>
<tr>
<td colspan="5" align="right"><b><font color="red">Total:</font><b></td>
<td align="right">£' . number_format ($total, 2) . '</td>
</tr>
</table>
<div align="center"><input type="submit" name="submit" value="Update Shopping Cart" />
</form><br>
<hr>
<a href="checkout.php"><font size="+1">Proceed to Checkout</font></a>&nbsp;&nbsp;&nbsp;
<a href="clear_cart.php"><font size="+1">Clear the Cart Contents</font></a><hr>
<a href="</div>';

mysql_close(); // Close DB Connection.

} else {
echo '<p align="center"><b>Your shopping cart is currently empty.</p><hr>';
}
include ('footer_on.html');
Post Reply