PHP- Mysql条件并不总是评估相同

时间:2015-04-16 23:19:32

标签: php mysql if-statement

我有这个代码,它进行简单的查询,然后根据结果执行各种条件。

变量$_SESSION['nombre'];可能不对,因为之前使用数据库检查了分配值的页面。

变量$_POST['evaluado'];也可能出错,因为它是直接从数据库中的已验证列表部署的。

问题是某些用户(并非所有用户)都收到了条件mysqli_num_rows($result) > 0不正确时显示的消息。

我无法重现此错误,我已经运行此代码的用户名和密码据称给出了这个错误,但它对我来说完全正常,我无法重现错误,但我确实得到了截图正在向用户显示的错误。

您是否在我的代码中看到任何可能导致有时条件为真的情况,有时情况并非如此?

<?php
session_start();
header('Content-Type: text/html; charset=UTF-8');

$evaluador=$_SESSION['nombre'];
$evaluado=$_POST['evaluado'];

// Create connection
$conn = mysqli_connect("localhost", "sbp_admin", "sbp2014", "sbp_encuesta90");
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT Evaluador, Evaluado,Realizado FROM Beta WHERE Evaluador='$evaluador' AND Evaluado='$evaluado'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        if ($evaluado==$row['Evaluado']) {
            # code...
             if($row['Realizado'] == 0){
        $_SESSION['nombre']=$evaluador;
        $_SESSION['evaluado']=$evaluado;
        //echo $evaluado;
            header('Location: evaluacion.php');
    }
    else
    {
    echo "<!DOCTYPE html>";
    echo '<head><meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css">
<script type="text/javascript">
 $(function() {
        /* For zebra striping */
        $("table tr:nth-child(odd)").addClass("odd-row");
        /* For cell text alignment */
        $("table td:first-child, table th:first-child").addClass("first");
        /* For removing the last border */
        $("table td:last-child, table th:last-child").addClass("last");
        $("table:last-child").addClass("last");
});
</script>
</head>
<body>
    <div style="width:1000; height:92px;margin: 0 auto;display: inline-block;"><div style="width:382; height:92px; display: inline-block;float: left;"><img src="logo.gif"></div><div style="width:328; height:41px; margin-top: 30px; display: inline-block;float: right;"><img src="logo.png"></div></div>
    <div><h1 style="float:left;">Evaluacion de Liderazgo 90&deg;</h1>
        <br><br>
        <br><br>
        <br><br><br><br><br><br>
        <p>Ya has evaluado a esta persona, <a href="index.html">regresa</a> a la p&aacute;gina principal de nuestra encuesta de liderazgo</p>
<br><br></body>';
        }
    }
        else{
        echo "<!DOCTYPE html>";
       echo '<head><meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css">
<script type="text/javascript">
 $(function() {
        /* For zebra striping */
        $("table tr:nth-child(odd)").addClass("odd-row");
        /* For cell text alignment */
        $("table td:first-child, table th:first-child").addClass("first");
        /* For removing the last border */
        $("table td:last-child, table th:last-child").addClass("last");
        $("table:last-child").addClass("last");
});
</script>
</head>
<body>
    <div style="width:1000; height:92px;margin: 0 auto;display: inline-block;"><div style="width:382; height:92px; display: inline-block;float: left;"><img src="logo.gif"></div><div style="width:328; height:41px; margin-top: 30px; display: inline-block;float: right;"><img src="logo.png"></div></div>
    <div><h1 style="float:left;">Evaluacion de Liderazgo 90&deg;</h1>
        <br><br>
        <br><br>
        <br><br><br><br><br><br>
        <p>No puedes evaluar a esta persona, <a href="index.html">regresa</a> a la p&aacute;gina principal de nuestra encuesta de liderazgo</p>
<br><br></body>';
        }
} 
}
else
{
echo "<!DOCTYPE html>";
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css">
<script type="text/javascript">
 $(function() {
        /* For zebra striping */
        $("table tr:nth-child(odd)").addClass("odd-row");
        /* For cell text alignment */
        $("table td:first-child, table th:first-child").addClass("first");
        /* For removing the last border */
        $("table td:last-child, table th:last-child").addClass("last");
        $("table:last-child").addClass("last");
});
</script>
</head>
<body>
    <div style="width:1000; height:92px;margin: 0 auto;display: inline-block;"><div style="width:382; height:92px; display: inline-block;float: left;"><img src="logo.gif"></div><div style="width:328; height:41px; margin-top: 30px; display: inline-block;float: right;"><img src="logo.png"></div></div>
    <div><h1 style="float:left;">Evaluacion de Liderazgo 90&deg;</h1>
        <br><br>
        <br><br>
        <br><br><br><br><br><br>
        <p>Verifica tu correo ya que el que ingresaste no esta en nuestra base de datos, <a href="index.html">regresa</a> a la p&aacute;gina principal de nuestra encuesta de liderazgo</p>
<br><br></body>';
}
mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:0)

如果不是这样,则表示查询返回0行。

您是否完全确定每个可能的EvaluadorEvaluado对都至少要返回一行?

您还可以检查添加此条件的查询中是否存在错误:

...
$result = mysqli_query($conn, $sql);
if (!$result) {
   // Display error and store the pair of inputs for debugging
}
else if (mysqli_num_rows($result) > 0) {
...