|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
PHP and MySQL script problem. Please help, urgent!
Hi, I'm building a shopping cart using PHP and MySQL. I've done most of the stuff needed but I am having a problem when it comes to adding items to the cart. If I click add to cart, the program says it has done so. But when I go to the cart, it says that it is empty. I have a feeling I've done something wrong with the sessions or with the MySQL query. Could anyone please help me? Here's the code for the addcart.php file:
<?php $page_title = 'Add to Cart'; if (isset ($_GET['pid'])) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; // Add another. // Display a message. echo '<p>Another copy of the print has been added to your shopping cart.</p>'; } else { // New product to the cart, get the price information. require_once ('sqlconnect.php'); // Connect to the database. $query = "SELECT price FROM product WHERE product.product_id = $pid"; $result = mysqli_query ($dbc, $query); if (mysqli_num_rows($result) == 1) { list($price) = mysqli_fetch_array ($result, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price); echo '<p>The product has been added to your shopping cart. If you want to purchase travel for this event, please <a href="browse_travel.php"> Go Here </a> Otherwise, go straight to the <a href="view_cart.php">shopping cart </a></p>'; } else { echo '<div align="center">This page has been accessed in error!</div>'; } mysqli_close($dbc); } // End of isset($_SESSION['cart'][$pid] conditional. } else { // No print ID. echo '<div align="center">This page has been accessed in error!</div>'; } ?> And here's the code for the shopping cart: <?php $page_title = 'View Your Shopping Cart'; if (isset($_POST['submitted'])) { // Change any quantities. foreach ($_POST['qty'] as $k => $v) { // Must be integers! $pid = (int) $k; $qty = (int) $v; if ( $qty == 0 ) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ( $qty > 0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } // End of FOREACH. } // End of SUBMITTED IF. // Check if the shopping cart is empty. $empty = TRUE; if (isset ($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; break; // Leave the loop. } } // End of FOREACH. } // End of ISSET IF. // Display the cart if it's not empty. if (!$empty) { require_once ('sqlconnect.php'); // Retrieve all of the information for the prints in the cart. $query = "SELECT * FROM category, product WHERE category.cat_id = product.cat_id AND product.product_id = $pid IN ("; foreach ($_SESSION['cart'] as $pid => $value) { $query .= $pid . ','; } $query = substr ($query, 0, -1) . ') ORDER BY product.product_name ASC'; $result = mysqli_query ($dbc, $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>Product Type</b></td> <td align="left" width="30%"><b>Product 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 = mysqli_fetch_array ($result, MYSQLI_ASSOC)) { // Calculate the total and sub-totals. $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price']; $total += $subtotal; // Print the row. echo " <tr> <td align="left">{$row['cat_name']}</td> <td align="left">{$row['product_name']}</td> <td align="right">${$_SESSION['cart'][$row['product_id']]['price']}</td> <td align="center"><input type="text" size="3" name="qty[{$row['product_id']}]" value="{$_SESSION['cart'][$row['product_id']]['quantity']}" /></td> <td align="right">$" . number_format ($subtotal, 2) . "</td> </tr>n"; } // End of the WHILE loop. mysqli_close($dbc); // 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="submitted" 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>'; } ?> Any help would be much appreciated, thanks! |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Installation > PHP and MySQL script problem. Please help, urgent! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|