为什么当事务为空时不显示EMPTY,为什么当代码相同时,不同用户的输出不同(在同一页面上)

时间:2014-01-18 16:33:45

标签: php

我是php的新手,我希望你们能理解我在这里想说的话。

我不明白为什么使用相同的代码,但不同用户的输出完全不同。

我的代码是这样的,如果我的事务不是EMPTY,它将回显出项目,ELSE它将回显“空”。 这段代码适用于我的这个用户(john) 但是,当另一个用户(david)使用它时,输出是不同的。 将有多个“空”回声。

我想知道为什么会这样? 因为对于约翰来说,“空”只会回响一次。 但是对于大卫来说,“空”回声多次出现。 但编码在同一页面上。

我还有另一个问题。 在我将代码更改为以下代码之后,如下所示, 当我的购物车为空时,不显示“空”。 为什么会这样?

这是我的编码

<?php
do{

$des = $row_supermarketcart['productdes'];
$pack = $row_supermarketcart['package'];
$price = $row_supermarketcart['itemprice'];
$qty = $row_supermarketcart['qty'];
$ddate  = $row_supermarketcart['ddate']   ;

if ($row_supermarketcart['username'] == $_SESSION['MM_Username'] )
{

            echo "<tr>";
            echo "<td>". $des ."</td>";
            echo "<td>". $pack. "</td>";
            echo "<td>$" .$price. "</td>";
            echo "<td>". $qty. "</td>";
            echo "<td>".$ddate."</td>";



}

else
{
     echo "<br>";
      echo "<center><b>YOU HAVE NOT MAKE ANY TRANSACTION YET</b></center>";
      echo "<br>";
      echo "<br>";

}

} while ($row_supermarketcart = mysql_fetch_assoc($supermarketcart));
?>

我的完整编码

 <?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);
//$sql_username = mysql_real_escape_string($_SESSION['MM_Username']);
$query_supermarketcart = "SELECT * FROM supermarketcart ORDER BY `ddate` DESC";
//$query_supermarketcart = "SELECT * FROM supermarketcart ORDER BY `ddate` DESC;";
$supermarketcart = mysql_query($query_supermarketcart, $MyDatabase) or die(mysql_error());
$row_supermarketcart = mysql_fetch_assoc($supermarketcart);
$totalRows_supermarketcart = mysql_num_rows($supermarketcart);
$query_cart = "SELECT * FROM user_data";
$cart = mysql_query($query_cart, $MyDatabase) or die(mysql_error());
$row_cart = mysql_fetch_assoc($cart);
$totalRows_cart = mysql_num_rows($cart);
?>


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Your Past Transaction</title>
</head>

<body background="background.jpg">
<table width="1024" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#EBF4FA">
  <tr>
    <td><img src="logo.png" width="450" height="86" hspace="50"> 
    </td>
    <td>
      <blockquote><h4><?php echo "Welcome,".($_SESSION['MM_Username']) ?></h4> </blockquote>
  <blockquote><img src="cart.png" alt="cart" width="35" border="0" usemap="#Map">
    <a href="feedback.php">Feedback</a>
     <a href="<?php echo $logoutAction ?>">Logout</a>

    </blockquote></td>
  </tr>
</table>
<br>
<table width="1024" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#EBF4FA">
 <tr>
 <td><img src="back.png" alt="back"  height="30" border="0" usemap="#Map">
 <img src="cart-navigation.png"  height="20" border="0" usemap="#Map2" > 
 <h2>Past Transaction</h2>   
 <hr></td>
 </tr>

</table>
<table width="1024" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#EBF4FA">
 <tr>
 <td>

<table width="800" border="1" align="center" cellpadding="5" cellspacing="0" bgcolor="#EBF4FA">
<tr>
<td>Item</td>
<td>Package</td>
<td>Price</td>
<td>Quantity</td>
<td>Date of Purchase</td>

</tr>
<?php
if (mysql_num_rows($supermarketcart) > 0)
{
do{

$des = $row_supermarketcart['productdes'];
$pack = $row_supermarketcart['package'];
$price = $row_supermarketcart['itemprice'];
$qty = $row_supermarketcart['qty'];
$ddate  = $row_supermarketcart['ddate']   ;


if ($row_supermarketcart['username'] == $_SESSION['MM_Username'] )
{

            echo "<tr>";
            echo "<td>". $des ."</td>";
            echo "<td>". $pack. "</td>";
            echo "<td>$" .$price. "</td>";
            echo "<td>". $qty. "</td>";
            echo "<td>".$ddate."</td>";



}
} while ($row_supermarketcart = mysql_fetch_assoc($supermarketcart));
}

else
{
     echo "<br>";
      echo "<center><b>YOU HAVE NOT MAKE ANY TRANSACTION YET</b></center>";
      echo "<br>";
      echo "<br>";

}


?>

<!--<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>-->

</tr>
</table>

</td>
</tr>
</td>

</tr>
</table>

1 个答案:

答案 0 :(得分:0)

你的sql看起来像:

$query_supermarketcart = "SELECT * FROM supermarketcart ORDER BY `ddate` DESC;";

因此,您将从数据库中获取所有行,您只需要登录用户的行。

你应该在sql中做些什么:

$sql_username = mysql_real_escape_string($_SESSION['MM_Username']);
$query_supermarketcart = "SELECT * FROM supermarketcart WHERE username={$sql_username} ORDER BY `ddate` DESC";

现在,您只在结果集中拥有已登录用户的行,以便执行以下操作:

if (mysql_num_rows($supermarketcart) > 0)
{
  do {

    // your whole cart echo code

  } while ($row_supermarketcart = mysql_fetch_assoc($supermarketcart));
}
else
{
     echo "<br>";
     echo "<center><b>YOU HAVE NOT MAKE ANY TRANSACTION YET</b></center>";
     echo "<br>";
     echo "<br>";
}

请注意mysql_*函数,您应该使用预准备语句切换到PDO或mysqli。