其他语句在选项选择中不起作用

时间:2015-03-25 12:47:37

标签: php mysql sql

我正在尝试实施下拉搜索选项。我的所有搜索结果都有效。我分配给if语句的所有命令都有效,但是当它对其他语句有效时,它无法正常工作。

这是我的代码:

if(isset($_REQUEST['submit'])){

    $opt = $_POST['opt'];

    if($opt==1){//if opt = 1
        $sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
        $resulte = mysql_query($sqle,$con) or die(mysql_error());

        while($row=mysql_fetch_array($resulte)){
            echo "<h4>" . $row['title'] . "</h4><br/>";
            echo "<p>" . $row['description'] . "<p>";

        }
    }else if($opt==2){//if opt = 2
        $sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
        $results = mysql_query($sqls,$con)or die(mysql_error());

        while($row=mysql_fetch_array($results)){
            echo "<h4>" . $row['games_name'] . "</h4><br/>";
            echo "<p>" . $row['description'] . "<p>";
        }
    }else{

        echo "Your Searched keyword did not match";
    }           
}

怎么办?

1 个答案:

答案 0 :(得分:1)

试试这个:拿一个标志来检查记录是否存在。

$flag = false;

if($opt==1){//if opt = 1
    $sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
    $resulte = mysql_query($sqle,$con) or die(mysql_error());

    if(mysql_num_rows($resulte) > 0) {

      $flag = true;
      while($row=mysql_fetch_array($resulte)){
      echo "<h4>" . $row['title'] . "</h4><br/>";
      echo "<p>" . $row['description'] . "<p>";

      }

    }

}else if($opt==2){//if opt = 2
    $sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
    $results = mysql_query($sqls,$con)or die(mysql_error());
    if(mysql_num_rows($resulte) > 0) {
      $flag = true;
      while($row=mysql_fetch_array($results)){
      echo "<h4>" . $row['games_name'] . "</h4><br/>";
      echo "<p>" . $row['description'] . "<p>";
      }
    }
}


if(!$flag){
    echo "Your Searched keyword did not match";
}