Php 我的项目与id名称不同

Php 我的项目与id名称不同,php,Php,我在做购物车的教程。 然而, 我得到的结果与id不一样。 意思 当我想显示项目的名称时, 虽然身份证不同,但我得到的名字和价格都是一样的。 为什么会这样 这是我的代码 <?php require_once('Connections/MyDatabase.php'); ?> <?php //initialize the session if (!isset($_SESSION)) { session_start(); } // ** Logout the current

我在做购物车的教程。 然而, 我得到的结果与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>";


}
}
}

?>


这是因为进入这一行:

while($row = mysql_fetch_array($sql)){
您有一个代码“following”

这将不会从db select查询中获取数据。这是从另一个地方拍的。解决此问题将解决您的问题,例如:

$product_des = $row['description'];
$price = $row['price'];
编辑:在阅读了您的一些代码后,我发现您还有其他问题,如有必要,我将稍后发布一些其他代码以修复正确数据的视图

要使系统动态化,您需要在其中加入一些循环“想法”:

while($row=mysql\u fetch\u数组($sql)){
$product_des=$row['description'];
$price=$row['price'];
$cartOutput.=“购物车项目$i”;
$cartOutput.=“项目ID:”$each_Item['ID']。“
”; $cartOutput.=“项目数量:”.$each_Item['quantity']。“
”; $cartOutput.=“项目名称:”.$product_des.“
”; $cartOutput.=“物品价格:”.$Price.“
”; } }

$cartOutput需要放在循环中才能看到所有正确的内容,据我所知,这可能是不必要的…

我的id没有显示,我现在有很多问题。项目不在我的文件夹中cart@user3156220在代码的开头有一个会话_start()?我哪儿也看不到。有。。它与login@user3156220你发布的代码中没有。在将所有代码放入所有页面之前,您需要使用它。您是否进行了会话变量转储以查看会话是否包含数据?您好,我刚刚对代码做了一些警告。现在声明:未定义索引:item_id&&当我添加到购物车时,添加的第一个项目不会显示,当我再次添加时,它将显示
$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>";


}
}