【问题标题】:Why no output of this code? Where is the error?为什么这段代码没有输出?错误在哪里?
【发布时间】:2015-05-29 10:41:32
【问题描述】:

这是我的 php 网站上的代码,除了显示登录用户的标题菜单外,该页面显示为空白。我只是找不到错误。 由于favitems 表是空的,它应该显示您没有任何产品信息。

<?php
include("includes/config.php");
include 'includes/header.php';

if(!isset($_SESSION['username'])){

    header("Location:login.php");

}else{

    $test = mysql_query("SELECT * FROM favitems 
        WHERE id = '".$_SESSION['username']."'") 
        or die("ERROR :<hr>".mysql_error());

    if($test == NULL){

        echo "<div class=products>";
        echo "<br>You have no products to show!";
        echo "</div>";

    } else {

        While ($row = mysql_fetch_array($test)){

            echo "<div class='products'>";

            $id        = $row['itemid'];
            $query     = mysql_query("SELECT * FROM products 
                                   WHERE id =".$id);
            $q         = mysql_fetch_array($query);

            echo $q['item_name']." "; echo $q['brand_name']."<br>";
            echo $q['qty']."<br>";
            echo $q['category']."<br>"; 
            echo $q['state']."<br>";
            echo "<br><a href=view.php?id=".$q['item_id'].">
            View item</a>";

        }   
        echo "</div>";


    }
}
include("includes/footer.php");
?>

【问题讨论】:

  • mysql_query() 将在失败时返回 FALSE 而不是 NULL
  • 我们能看到 HTML 输出吗?
  • 当前的错误设置是什么?您应该尝试做的第一件事是查看错误日志以找出问题所在。
  • 这不会回答问题,但这是一个安全问题。将此header("Location:login.php"); 更改为此header("Location:login.php"); exit();。如果你不这样做,其余的代码仍然会被执行。
  • 顺便说一句,看看使用mysqli 代替已弃用的mysql。此外,您的代码查询容易受到 SQL 注入的攻击,因此要么通过 mysql_real_escape_string(最好是 mysqli_real_escape_string)传递变量,要么使用准备好的语句。

标签: php mysql loops


【解决方案1】:
if(!isset($_SESSION['username'])){
      header("Location:login.php");
      exit;
}

 1. Execute your query here 
 2. If error found throw Error message 
 3. If successful execution, Check no of rows fetched


  if(row_fetched > 0 )  {
        // print table
  } else {
    // print no recors found
  }


 include("includes/footer.php");

还要检查您在此处使用的所有内置函数的变量范围和返回值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 2012-03-25
    • 2016-04-20
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    相关资源
    最近更新 更多