下拉列表和输入搜索框。不搜索数据库并提取0结果。

时间:2018-05-28 14:41:37

标签: php html mysql

希望你们能提供帮助。我正在创建一个搜索栏,允许用户选择要搜索的字段以及键入搜索字词。然而,似乎当他们搜索它时会产生"没有找到记录"。任何人都可以看看我的代码,看看我可能出错的地方?非常感谢你!

HTML表单



   	<form name="search" method="post" action='recordresult.php' class="form-style-1">
	 Search for: <input type="text" name="find" /> in

	 <Select NAME="field">
  	 <Option VALUE="first_name">First Name</option>

	 <Option VALUE="surname">Surname</option>

	 <Option VALUE="address">Address</option>
	 <Option VALUE="phonenumber">Phone Number</option>

	 </Select>

	 <input type="hidden" name="searching" value="yes" />

	 <input type="submit" name="search" value="Search" />

	 </form>
&#13;
&#13;
&#13;

Php

&#13;
&#13;
<?php
include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysqli_error($link));

// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));

//get information
$field = $_POST['field'] ;
$find = $_POST['find'] ;
$searching = $_POST['searching'] ;

 //once submitted  
 if ($searching =="yes") 
 { 
 echo "<h2>Results</h2><p>"; 
 

 //search database 
 $data = mysqli_query("SELECT * FROM characters WHERE $field LIKE'%$find%'"); 
 
 //display the results 
 while($result = mysqli_fetch_array( $data )) 
 { 
 echo "<div class='result'><b>";
 echo $result['first_name']; 
 echo "</b><br/>"; 
 echo $result['surname']; 
 echo "<br/>"; 
 echo $result['address']; 
 echo "<br/>"; 
 echo $result['phonenumber']; 
 echo "<br/>"; 
 echo "</div>"; 
 } 
 
 //number of results or error 
 $anymatches=mysqli_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 
 
 //the search term 
 echo "<br><b>Searched For:</b> " .$find; 
  } 
?>
	
&#13;
&#13;
&#13;

尝试过&#39;&#39;关于字段名称变量的查询也有相同的结果。检查变量中的字段名称,并在其中生成正确的字段。

感谢您提供的所有帮助!

1 个答案:

答案 0 :(得分:0)

在某些情况下,请尝试使用“。”进行查询。

$data = mysqli_query($link, "SELECT * FROM characters WHERE $field LIKE '%".$find."%'");