doujiao1984 2011-10-27 16:26
浏览 7
已采纳

将有效变量传递给查询时出现问题

I'm passing variables to my page page?title=fred but I would like to redirect to another page if it's the wrong value(not in the database) ex: fredd


The first part checks if the variable is empty. Which seems to be working.

//get variable from title
if(!isset($_GET['title']) || empty($_GET['title'])) {
    echo "no variable";
} 
else {
    $get_title = mysqli_real_escape_string($db, $_GET['title']);
    $title = str_replace("-"," ", $get_title);
} 


I'm having trouble checking if the variable I pass is in the database and if it's not to redirect to main page.

$query = mysqli_query($db, "SELECT title FROM persons WHERE title = '$title'");
//check that variable is in the database
if(!isset($query)){
  while($row = mysqli_fetch_assoc($query)){

    echo $row['title'];

  }
// mysqli_free_result($query);
} 
else {echo "variable does not exist";} ?>


Can someone please show me what I am doing wrong and how to fix it? Thank you,

  • 写回答

2条回答 默认 最新

  • dswe30290 2011-10-27 16:30
    关注
    if(!isset($query)){
    

    $query is always set (you just set it) so the code you want will never execute.

    You want this instead:

    if (!$query) {
      echo "Query returned error";
    } else if (mysqli_num_rows($query) < 1) {
      echo "Variable does not exist";
    } else {
      while ($row = mysqli_fetch_assoc($query)){
        echo $row['title'];
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致