<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$name = isset($_GET['NameOfTheDVD']) ? ($_GET['NameOfTheDVD']): "";
$DVDID = isset($_GET['DVDID']) ? ($_GET['DVDID']) : "";
$Quantity =isset($_GET['Quantity']) ? ($_GET['Quantity']) : "";
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='removed'){
echo "<div>" . $_GET['NameOfTheDVD'] . " was removed from cart.</div>";
}
if(isset($_SESSION['cart'])){
$ids = " ";
foreach($_SESSION['cart'] as $DVDID){
}
// remove the last comma
$ids = rtrim($ids, ',');
require "connect.php";
$query = "SELECT DVDID, NameOfTheDVD, Quantity FROM DVD WHERE DVDID IN ({$ids})";
$stmt = $dbhandle->prepare( $query );
$stmt->execute();
$num = $stmt->rowCount();
if($num>0){
echo "<table border='0'>";//start table
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Quantity</th>";
echo "<th>Action</th>";
echo "</tr>";
//also compute for total price
$totalQuantity = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$totalQuantity += $Quantity;
//creating new table row per record
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td class='textAlignRight'>{$Quantity}</td>";
echo "<td class='textAlignCenter'>";
echo "<a href='RemoveFromCart.php?DVDID={$DVDID}&name={$name}' class='customButton'>";
echo "<img src='images/remove-from-cart.png' title='Remove from cart' />";
echo "</a>";
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<th class='textAlignCenter'>Total Price</th>";
echo "<th class='textAlignRight'>{$totalQuantity}</th>";
echo "<th></th>";
echo "</tr>";
echo "</table>";
echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
}else{
echo "<div>No products found in your cart. :(</div>";
}
}else{
echo "<div>No products in cart yet.</div>";
}
?>
这是产品要显示但不显示的地方。我不知道问题是什么。我不知道是否应该有一些if语句检查如果不显示结果会话是否为空。 Stack溢出有2个与此相关的主题,但我认为它们没有得到解决。
这是我的AddtoCart.php
<?php
session_start();
// get the product id
$DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
$name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
. */
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
// check if the item is in the array, if it is, do not add
if(in_array($DVDID, $_SESSION['cart'])){
// redirect to product list and tell the user it was added to cart
header('Location: shop.php?action=exists&DVDID' . $DVDID . '&name=' . $name);
}
// else, add the item to the array
else{
array_push($_SESSION['cart'], $DVDID);
// redirect to product list and tell the user it was added to cart
header('Location: shop.php?action=add&DVDID' . $DVDID . '&name=' . $name);
}
?>
感谢您提供的任何帮助