duan0531 2016-07-24 09:24
浏览 45
已采纳

PHP:if-else语句和mysql_num_rows不起作用

I'm working on a web page where a user will be allowed to add or edit a feedback for a place. If the person already gave the place a feedback, he/she will be shown an Edit Review button otherwise, the user will be shown an Add Review button.

In my feedback table in my database, I have a places_id and users_id column. Here's my query in PHP (Assuming that $id and $places_id have values):

<?php
$query5 = mysql_query("SELECT * FROM feedback WHERE user_id = '$id' AND places_id = '$places_id'");
$row5 = mysql_fetch_array($query5);

    if($row5['user_id'] != $id){ 
?>

    <a href="trip-advisor.php"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span> Back</button></a>
    <button class="btn btn-primary" type="submit" name="submit">Review</button>

    <?php }else{ ?>

    <a href="trip-advisor.php"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span> Back</button></a>
    <button class="btn btn-primary" type="submit" name="submit">Edit Review</button>

<?php } ?>

I was thinking that if the query did not return a result where the given user_id is not equal to the user_id in the result, it would let me do my if-else condition. However if there is a feedback for a current place, it displays the button found in the else statement.

What do you think is the problem and why is the if condition not working?

I also tried if(mysql_num_rows($query5)==0) but it's also not working.

  • 写回答

1条回答 默认 最新

  • douweng7083 2016-07-24 09:37
    关注

    I would always do:

    <?php
    $query5 = mysql_query("SELECT * FROM feedback WHERE user_id = '$id' AND places_id = '$places_id'");
    if ($row5 = mysql_fetch_array($query5)) {
    ?>
    
    <a href="trip-advisor.php"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span> Back</button></a>
    <button class="btn btn-primary" type="submit" name="submit">Edit Review</button>
    
    <?php }else{ ?>
    
    <a href="trip-advisor.php"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span> Back</button></a>
    <button class="btn btn-primary" type="submit" name="submit">Review</button>
    
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题