我正在做购物车教程。 然而, 我得到的结果与id不一样。 含义, 当我想显示项目的名称时, 虽然id不同,但我回来的名字和价格都是一样的。 为什么会这样?
这是我的编码。
<?php require_once('Connections/MyDatabase.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "login.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_MyDatabase, $MyDatabase);
$query_supermarketDetails = "SELECT * FROM supermarket";
$supermarketDetails = mysql_query($query_supermarketDetails, $MyDatabase) or die(mysql_error());
$row_supermarketDetails = mysql_fetch_assoc($supermarketDetails);
$totalRows_supermarketDetails = mysql_num_rows($supermarketDetails);
?>
<?php require_once('Connections/MyDatabase.php'); ?>
<?php
//script error reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
?>
<?php
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["supermarketcart"]) || count($_SESSION["supermarketcart"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["supermarketcart"][] = array(1 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["supermarketcart"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["supermarketcart"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["supermarketcart"], array("item_id" => $pid, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
?>
<?php
//if user choose to empty cart
if(isset($_GET['cmd']) && $_GET['cmd'] == "emptycart")
{
unset($_SESSION["supermarketcart"]);
}
?>
<?php
//render the cart for user to view
$cartOutput = "";
if(!isset($_SESSION["supermarketcart"]) || count($_SESSION["supermarketcart"]) < 1 ){
$cartOutput .= "<h2 align = 'center'> Your shopping cart is empty</h2>";
}
else
{
$i = 0;
foreach ($_SESSION["supermarketcart"] as $each_item)
{
$i++;
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM supermarket WHERE id = '$item_id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$product_des = $row['description'];
$price = $row['price'];
$cartOutput .= "<h2>Cart Item $i</h2>";
$cartOutput .= "Item ID: " . $each_item['item_id']."<br>";
$cartOutput .= "Item Quatity: " . $each_item['quantity']."<br>";
$cartOutput .= "Item Name: " . $product_des."<br>";
$cartOutput .= "Item Price: " . $price."<br>";
}
}
}
?>
答案 0 :(得分:0)
那是因为进入这一行:
while($row = mysql_fetch_array($sql)){
你有一个代码“跟随”
$product_des = $row_supermarketDetails['description'];
$price = $row_supermarketDetails['price'];
不会从数据库选择查询中获取数据。它是从另一个地方拍摄的。修复此问题可以解决您的问题,例如:
$product_des = $row['description'];
$price = $row['price'];
编辑:在阅读了一些代码后,我发现您还有其他问题,如果需要,我会稍后发布一些其他代码来修复正确数据的视图。
要使系统动态化,你需要在其中添加一些循环“想法”:
while($row = mysql_fetch_array($sql)){
$product_des = $row['description'];
$price = $row['price'];
$cartOutput .= "<h2>Cart Item $i</h2>";
$cartOutput .= "Item ID: " . $each_item['id']."<br>";
$cartOutput .= "Item Quatity: " . $each_item['quantity']."<br>";
$cartOutput .= "Item Name: " . $product_des."<br>";
$cartOutput .= "Item Price: " . $price."<br>";
}
}
$ cartOutput需要放在循环中以查看一切正确,据我所知,它可能是不必要的......