简体   繁体   English

MySQL数据库搜索PHP问题

[英]MySQL database search php issue

I have written basic script for search database of my current php site. 我已经为我当前的php网站的搜索数据库编写了基本脚本。 its showing me data even if I do not write any word in search box as well it show many row same for any keyword. 即使我没有在搜索框中输入任何单词,它也会显示我的数据,并且它对任何关键字都显示很多行。 as well its not getting refreshed for new search. 以及它不会为新搜索而刷新。 Please check and let me know what I have missed in this ? 请检查,让我知道我错过了什么? Thanks 谢谢

 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <?php include("config.php"); if($_SERVER["REQUEST_METHOD"] == "POST") { mysql_query('SET character_set_results=utf8'); mysql_query('SET names=utf8'); mysql_query('SET character_set_client=utf8'); mysql_query('SET character_set_connection=utf8'); mysql_query('SET character_set_results=utf8'); mysql_query('SET collation_connection=utf8_general_ci'); $q=$_POST['q']; $q=mysql_escape_string($q); $q_fix=str_replace(" ","%",$q); // Space replacing with % $sql=mysql_query("SELECT qu_text FROM quotes WHERE qu_text LIKE N'%$qu_text%'"); } ?> <html> <body> <form method="post" action=""> <input type="text" name="q" /> <input type="submit" value=" Search " /> </form> <?php while($row=mysql_fetch_array($sql)) { $title=$row['qu_text']; echo '<div>'.$title.'</div>'; } ?> </body> </html> 

Your SQL is wrong. 您的SQL错误。

The variable $qu_text is not initialised anywhere. 变量$qu_text不会在任何地方初始化。

SELECT qu_text FROM quotes WHERE qu_text LIKE N'%%'

You have added wild card like on $q_text . 您已经在$q_text上添加了通配符。 It does not get any value, therefore, it is showing all results. 它没有任何值,因此显示所有结果。

As the query is becoming: 随着查询变得越来越:

Correct Query: 正确查询:

$sql=mysql_query("SELECT qu_text FROM quotes WHERE qu_text LIKE '%$q%'");

As you are doing a wild card search - if $qu_text is empty your query will return everything. 在进行通配符搜索时-如果$ qu_text为空,则查询将返回所有内容。 If your requirement is to show nothing if the search parameter is blank. 如果您的要求是在搜索参数为空白的情况下不显示任何内容。 Then put a condition in and do not even perform the query if $qu_text is null or empty. 然后放入一个条件,如果$ qu_text为null或为空,甚至不执行查询。

Also I think you need to pass $q_fix not $qu_text to the query. 另外我认为您需要将$ q_fix而不是$ qu_text传递给查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM