我在向购物车添加产品时遇到问题。为了更容易阅读,我只是提出了PHP代码。我现在并不担心安全问题,只是想通过页面view_cart.php上的购物车中显示的产品来解决问题。谁能看到我在这里失踪的东西?此外,会话在到达此点之前在另一页上启动。
<?php # add_cart.php
// This page adds beers to the shopping cart.
if (isset($_GET['beer_id'])) { // Check for a beer ID.
$beer_id = $_GET['beer_id'];
// Check if the cart already contains one of these beers;
// If so, increment the quantity:
if (isset($_SESSION['cart'][$beer_id])) {
echo '<p> same beer </p>';
$_SESSION['cart'][$beer_id]['quantity']++; // Add another.
// Display a message:
echo '<p> This brew was already in your cart so we added another to your shopping cart. </p>';
} else { // New product to the cart.
require ('mysqli_connect.php'); // Connect to the database.
$q = "SELECT price FROM beer WHERE beer_id='" . $beer_id . "'";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) { // Valid beer_ID.
// Fetch the information.
list($price) = mysqli_fetch_array ($r, MYSQLI_NUM);
// Add to the cart:
$_SESSION['cart'] = array('quantity' => 1, 'price' => $price);
echo $_SESSION['cart'][$beer_id][$r];
// Display a message:
echo '<p>' . $beer_id . 'has been added to your shopping cart.<br/><a href="view_cart.php">Go to Cart</a> or <a href="store.php">Keep Shopping</a></p>';
} else { // Not a valid beer_ID.
echo '<div align="center">This page has been accessed in error!</div>';
}
mysqli_close($dbc);
}
}// End of isset conditional.
else { // No beer_ID.
echo '<div align="center">This page has been accessed in error!</div>';
}
?>
以下页面是从add_cart.php
调用的<?php
# view_cart.php
// Check if the form has been submitted (to update the cart):
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Change any quantities:
foreach ($_POST['quantity'] as $k => $v) {
$beer_id = (int) $k;
$qty = (int) $v;
if ( $qty == 0 ) { // Delete.
unset ($_SESSION['cart'][$beer_id]);
} elseif ( $qty > 0 ) { // Change quantity.
$_SESSION['cart'][$beer_id]['quantity'] = $qty;
}
} // End of FOREACH.
} // End of SUBMITTED IF.
// Display the cart if it's not empty...
if (!empty($_SESSION['cart'])) {
// Retrieve information for beers in cart:
require ('mysqli_connect.php'); // Connect to the database.
$q = "SELECT beer_id, name, price FROM beer WHERE beer_id ='".$beer_id."'";
/*foreach ($_SESSION['cart'] as $beer_id => $value) {
$q .= $beer_id . ',';
}*/
$q = substr($q, 0, -1) . ') ORDER BY beer_id ASC';
$r = mysqli_query ($dbc, $q);
// Create a form and a table:
echo '<form action="view_cart.php" method="post">
<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>ID</b></td>
<td align="left" width="30%"><b>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>
';
// Print each item...
$total = 0; // Total cost of the order.
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
// Calculate the total and sub-totals.
$subtotal = $_SESSION['cart'][$row['beer_id']]['quantity'] * $_SESSION['cart'][$row['beer_id']]['price'];
$total += $subtotal;
// Print the row:
echo "\t<tr>
<td align=\"left\">{$row['name']}</td>
<td align=\"right\">\${$_SESSION['cart'][$row['beer_id']]['price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['beer_id']}]\" value=\"{$_SESSION['cart'][$row['beer_id']]['quantity']}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // End of the WHILE loop.
mysqli_close($dbc); // Close the db connection.
// Print the total, 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" /></div>
</form><p align="center">Enter a quantity of 0 to remove an item.
<br /><br /><a href="checkout.php">Checkout</a></p>';
} else {
echo '<form action="view_cart.php" method="post">
<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="10%"><b>ID</b></td>
<td align="left" width="30%"><b>Name</b></td>
<td align="right" width="30%"><b>Price</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="10%"><b>Total Price</b></td>
</tr></table>
';
echo '<p>Your cart is currently empty.</p>';
}
?>