我该怎么说,如果这个统计数据正在呈现,那么就展示它


How Do I Say If This Statment Is Being Presented Then Show THis Else SHow That

编辑

我现在得到一个错误:

  Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /home/jahedhus/public_html/system/vote.php on line 126

我的php代码如下:

    <?php 
$id = $_GET['election'];
$result = mysql_query("SELECT * FROM votes WHERE election_id = '$id' AND ni = '". $_SESSION['ni']."'" )
or die(mysql_error());
if (mysql_num_rows($result) == 0) {
   $sql="SELECT * FROM elections WHERE status = 'in_progress' AND election_id = '$id'";
$result1=mysql_query($sql);
$options="";
$party2="";
$party3="";
while ($row=mysql_fetch_array($result1)) {
    echo "<tr>";
    echo "<td><h5>" . $row['name_of_election']. "</h5><hr></td>";
    echo "</td>";

    $idd=$row["party1"];
    $thing=$row["party1"];
    $options.="<OPTION VALUE='"$idd'">".$thing;
    $idd=$row["party2"];
    $thing=$row["party2"];
    $party2.="<OPTION VALUE='"$idd'">".$thing; 
    $idd=$row["party3"];
    $thing=$row["party3"];
    $party3.="<OPTION VALUE='"$idd'">".$thing;
}
}
?>
<?php if (mysql_num_rows($result1) == 0) : ?>
  <form action="votecasted.php?election=<?php echo $id; ?>" method="post">
    <span id="spryselect1">
    <label for="vote">Vote</label>&nbsp;
    <select size=1" name="vote" id="vote">
        <option selected="selected">Select Your Desired Party</option>
        <?=$options?>
        <?=$party2?>
        <?=$party3?>
</select>
    <span class="selectRequiredMsg">*</span></span><br />
<br />    <input name="" type="submit" value="Vote" />
  </form>
<?php else : ?>
    You cannot post vote anymore.
<?php endif; ?>

126行显示:

<?php if (mysql_num_rows($result1) == 0) : ?>

这很奇怪,因为一旦用户投票,它就会显示错误消息,如果用户没有投票,它会说"你不能再发布投票了"。

如果我正确理解了这个问题,你只需要检查$resultfalse是否不同。我建议您对嵌入式PHP使用以下语法:

<?php if ($result) : ?>
    <form></form>
<?php else : ?>
    Something else
<?php endif; ?>

在您的案例中,使用更多的SQL注入检查:

<?php 
$id = mysql_real_escape_string($_GET['election']);
$ni = mysql_real_escape_string($_SESSION['ni']);
$votes = mysql_query("SELECT * FROM votes WHERE election_id = '$id' AND ni = '$ni'" )
or die(mysql_error());
if (mysql_num_rows($votes) == 0) {
    $sql = "SELECT * FROM elections WHERE status = 'in_progress' AND election_id = '$id'";
    $elections = mysql_query($sql);
    $options="";
    $party2="";
    $party3="";
    while ($row = mysql_fetch_array($elections)) {
        echo "<tr>";
        echo "<td><h5>" . $row['name_of_election']. "</h5><hr></td>";
        echo "</td>";
        $idd=$row["party1"];
        $thing=$row["party1"];
        $options.="<OPTION VALUE='"$idd'">".$thing;
        $idd=$row["party2"];
        $thing=$row["party2"];
        $party2.="<OPTION VALUE='"$idd'">".$thing; 
        $idd=$row["party3"];
        $thing=$row["party3"];
        $party3.="<OPTION VALUE='"$idd'">".$thing;
    }
}
?>
<?php if (mysql_num_rows($votes) == 0) : ?>
    <form action="votecasted.php?election=<?=$id?>" method="post">
        <span id="spryselect1">
        <label for="vote">Vote</label>&nbsp;
        <select size="1" name="vote" id="vote">
            <option selected="selected">Select Your Desired Party</option>
            <?=$options?>
            <?=$party2?>
            <?=$party3?>
        </select>
        <span class="selectRequiredMsg">*</span></span><br />
        <br />
        <input name="" type="submit" value="Vote" />
  </form>
<?php else : ?>
    You cannot post vote anymore.
<?php endif; ?>