Hi im fairly new to php and am stuck on building a shopping cart for an ecommerce photo website for my coursework. When i click on a product to add to the shopping cart, it says "The photo has been added to your cart". Which is great, BUT when i go to the shopping cart it doesnt display any products. It displays the shopping cart but its empty. Now i dont know if the problem is with the add_cart.php or the view_cart.php code, so i am showing both of them below.
<?php # add_cart.php
include('./header.html');
if (isset($_GET[imageid])){
$imageid = (int) $_GET['imageid'];
// Check if the cart already contains one of these prints, increment the quantity.
if (isset($_SESSION['shoppingcart'][$imageid])) {
$_SESSION['shoppingcart'][$imageid]['quantity']++; // Add another.
// Display a message.
echo '<p>Thank you. Another copy of the photograph has been added to your shopping cart.</p>';
} else { // New product to the cart, get the price information.
require_once('dbconnection.php'); //connecting to the DB
$query = "SELECT price FROM image WHERE imageid = $imageid";
$result = mysql_query($query);
if (mysql_num_rows($result) == 1) { // Valid print ID.
// Fetch the information.
list($price) = mysql_fetch_array ($result, MYSQL_NUM);
// Add to the cart.
$_SESSION['shoppingcart'][$imageid] = array ('quantity' => 1, 'price' => $price);
// Display a message.
echo '<p>The print has been added to your shopping cart.</p>';
} else { // Not a valid print ID.
echo '<div align="center">This page has been accessed in error!</div>';
}
mysql_close($connection);
} // End of isset($_SESSION['cart'][$pid] conditional.
} else { // No print ID.
echo '<div align="center">This page has been accessed in error!</div>';
}
include ('./footer.html');
?>
<?php # view_cart.php
// This page displays the contents of the shopping cart.
// This page also lets the user update the contents of the cart.
// Set the page title and include the HTML header.
//$page_title = 'View Your Shopping Cart';
include ('./header.html');
echo "<h2>Shopping cart</h2>";
// Check if the form has been submitted (to update the cart).
if (isset($_POST['submit'])) { // Check if the form has been submitted.
// Change any quantities.
foreach ($_POST['qty'] as $k => $v) {
// Must be integers!
$imageid = (int) $k;
$qty = (int) $v;
if ( $qty == 0 ) { // Delete.
unset ($_SESSION['cart'][$imageid]);
} elseif ( $qty > 0 ) { // Change quantity.
$_SESSION['shoppingcart'][$imageid]['quantity'] = $qty;
}
} // End of FOREACH.
} // End of SUBMITTED IF.
// Check if the shopping cart is empty.
$empty = TRUE;
if (isset ($SESSION['shoppingcart'])) {
foreach ($SESSION['shoppingcart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
break; // Leave the loop.
}
}
}
// Display the cart if it's not empty.
if (!$empty) {
require_once('dbconnection.php'); //connecting to the DB
// Retrieve all of the information for the prints in the cart.
$query = "SELECT (imageid, imagename, price) AS imageid, imagename, price FROM image WHERE imageid IN (";
foreach ($_SESSION['shoppingcart'] as $imageid => $value) {
$query .= $imageid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY imagename 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>Image ID</b></td>
<td align="left" width="30%"><b>Image Name</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 sub-totals.
$subtotal = $_SESSION['shoppingcart'][$row['imageid']]['quantity'] * $_SESSION['shoppingcart'][$row['imageid']]['price'];
$total += $subtotal;
// Print the row.
echo " <tr>
<td align=\"left\">{$row['imageid']}</td>
<td align=\"left\">{$row['imagename']}</td>
<td align=\"right\">\${$_SESSION['cart'][$row['imageid']]['price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['imageid']}]\" value=\"{$_SESSION['shoppingcart'][$row['imageid']]['quantity']}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // End of the WHILE loop.
mysql_close($connection); // Close the database connection.
// Print the footer, 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"><input type="submit" name="submit" value="Update My Cart" />
<input type="hidden" name="submit" value="TRUE" />
</form><br /><br /><a href="checkout.php"><font size="+2">Checkout</font></a></div>';
} else {
echo '<p>Your cart is currently empty.</p>';
}
echo "Note: To delete a photo from your shopping cart, change it's quantity to 0 and then click on upldate cart button.";
include ('./footer.html');
?>
I also get the errors on the view_cart.php page:
Warning: Invalid argument supplied for foreach() in /public_html/view_cart.php on line 13
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /public_html/view_cart.php on line 65
Can someone please tell me how i can fix this problem? Thank you in advance.