2015-09-03 79 views
0

我使用下拉列表创建搜索功能,用户可以根据所选下拉列表进行搜索并将其显示在同一页面上。当我按下提交按钮时,它没有显示任何结果。PHP:使用下拉列表搜索

对不起,如果这之前已经问过,但我怀疑有一些我在PHP代码做错了,但我不明白为什么。有人可以为我指出吗?

这是我的搜索形式:

<h2>Search</h2> 
<form name="search" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    Keyword &nbsp; 
    <input type="text" name="searchq" id="searchq" size="30" placeholder="Enter keyword..">&nbsp; By &nbsp; 
    <select name="searchopt" id="searchopt"> 
    <option value=""></option> 
    <option value="Members">Members</option> 
    <option value="Journal">Journal</option> 
    <option value="Conference">Conference</option> 
    <option value="Awards">Awards</option> 
    <option value="Grants">Grants</option> 
    <option value="Patents">Patents</option> 
    <option value="Research Grants">Research Grants</option> 
    <option value="Book Chapter">Book Chapter</option> 
    <option value="Book Publications">Book Publications</option> 
    <option value="Other Publications">Other Publications</option> 
    </select> 
    &nbsp; 
    <input type="submit" id="submit" name="submit" value="Search"> 
</form> 
<br> 
<p> 
    <h4>Results</h4> 
</p> 

,这是我的PHP代码:

include ("includes/dbcon.php"); 

if(isset($_POST['searchq']) && $_POST['searchq'] != "") 
{ 
    if(isset($_POST['searchopt']) && $_POST['searchopt'] != "") 
    { 
     $escsearch = mysqli_real_escape_string($conn, $_POST['searchq']); 
     $searchval = preg_replace('#[^a-z 0-9]#i', '', $escsearch); 
     $opt = $_POST['searchopt']; 

     switch($opt) 
     { 
      case "Members": 
      $query = "SELECT * FROM members where memberName LIKE '%$searchval%'"; 
      $res = mysqli_query($conn, $query) or die(mysqli_error()); 
      $count = mysqli_num_rows($res); 
      if($count > 1) 
      { 
       $output .= "$count results for <strong>$escsearch</strong>."; 

       while($row = mysqli_fetch_array($res)) 
       { 
        $name = $row['memberName']; 
        $pos = $row['position']; 
        $fac = $row['faculty']; 
        $email = $row['email']; 
        $int = $row['research_interests']; 

        echo "<table> 
          <tr> 
           <td>Name</td> 
           <td>Position</td> 
           <td>Faculty</td> 
           <td>E-mail</td> 
           <td>Research Interests</td> 
          </tr> 
          <tr> 
           <td>$name</td> 
           <td>$pos</td> 
           <td>$fac</td> 
           <td>$email</td> 
           <td>$int</td> 
          </tr> 
         </table>"; 
       } 
      }else{ 
       $output = "<p>No records found.</p>"; 
      } 
      break; 
     } 
    } 
} 
+0

$查询= “SELECT * FROM成员,其中成员名称= '$ searchval'” 为什么使用LIKE操作..尝试此查询 –

+0

var_dump($ _ POST ['searchq']);检查值发送到服务器 – Vishu238

+0

@丹尼尔南嗨,我试过了,但它不工作。 – Azie

回答

0

许多问题在这里:

  1. 你的提交按钮建议立即进行删除d没有name属性,因为你没有对它做任何事情。

    更改此:

    <input type="submit" id="submit" name="submit" value="Search"> 
    

    要这样:

    <input type="submit" id="submit" value="Search"> 
    
  2. if条件,检查结果如果结果发现返回false。

    更改此:

    if($count > 1) 
    

    要这样:

    if($count > 0) 
    
  3. 最后,您$output变量定义的错误的方式。使用.=假定$output先前已被定义。

    更改此:

    $output .= "$count results for <strong>$escsearch</strong>."; 
    

    要这样:

    $output = "$count results for <strong>$escsearch</strong>."; 
    
0

纠正你调理

if($count > 1) => if($count >= 1) 

好像你有只有一个r在的eCord数据库

还要定义$输出变量

0

如果只有一条记录与membername LIKE刘嘉玲,那么你将得到什么输出到页面。

该行说,必须有一个以上的结果对于脚本继续... if($count > 1)你可能希望它是​​

首先你要定义$输出为空字符串... $output = '';

希望这有助于