简体   繁体   English

使用 PHP 使用关键字创建高级搜索

[英]Creating advanced search with keyword using PHP

So I'm trying to create an advanced search using PHP and everything is going well with searching for title author and other variables, except for keyword.因此,我正在尝试使用 PHP 创建高级搜索,搜索标题作者和其他变量(关键字除外)一切顺利。 When its searching for a keyword it will display everything in the table instead of just those who have the keyword.当它搜索关键字时,它将显示表格中的所有内容,而不仅仅是那些拥有关键字的人。

here's my code这是我的代码

$sql="";
$title = $_GET['title'];
$author = $_GET['author'];
$isbn = $_GET['isbn'];
$publisher = $_GET['publisher'];
$keyword = $_GET['keyword'];



$limit = 5;  
if (isset($_GET["page"])) {
    $page  = $_GET["page"]; 
    } 
    else{ 
    $page=1;
    };  
$start_from = ($page-1) * $limit;  


if(isset($title)){
   $sql = "SELECT * FROM books WHERE title LIKE '%".$title."%' ";
   if(isset($author)){
     $sql .= "AND author LIKE '%".$author."%'";
   }
   if(isset($isbn)){
     $sql .= "AND isbn LIKE '%".$isbn."%'";
   }
   if(isset($publisher)){
     $sql .= "AND publisher LIKE '%".$publisher."%'";
   }
   $sql .= "ORDER BY title ASC LIMIT $start_from, $limit";
}
else if (isset($author)){ 
  $sql = "SELECT * FROM books WHERE author LIKE '%".$author."%' ";
   if(isset($isbn)){
      $sql .= "AND isbn LIKE '%".$isbn."%'";
   }
   if(isset($publisher)){
     $sql .= "AND publisher LIKE '%".$publisher."%'";
   }
   $sql .= "ORDER BY author ASC LIMIT $start_from, $limit";
}

else if(isset($isbn)){
   $sql = "SELECT * FROM books WHERE isbn LIKE '%".$isbn."%' ";
    if(isset($publisher)){
       $sql .= "AND publisher LIKE '%".$publisher."%'";
   }
   $sql .= "ORDER BY isbn ASC LIMIT $start_from, $limit";
}else if(isset($publisher)){
   $sql = "SELECT * FROM books WHERE publisher LIKE '%".$publisher."%' ORDER BY publisher ASC LIMIT $start_from, $limit";
   
}
else if(isset($keyword)){
   $sql = "SELECT * FROM books where (title LIKE '%".$keyword."%') OR (author LIKE '%".$keyword."%') OR (isbn LIKE '%".$keyword."%') OR (publisher LIKE '%".$keyword."%') ORDER BY title ASC LIMIT $start_from, $limit";

}


$bookselect = mysqli_query($conn, $sql);

i don't know if advanced searching using keyword is working or not, any clue why it is happening?我不知道使用关键字的高级搜索是否有效,任何线索为什么会发生?

From comments, I saw that you would get all the posts if you are putting nothing in the keyword.从评论中,我看到如果您没有在关键字中添加任何内容,您将获得所有帖子。 The reason for this behaviour is that your sql would seem something like this (replacing $keyword with ""):这种行为的原因是您的 sql 看起来像这样(将 $keyword 替换为“”):

 $sql = "SELECT * FROM books where (title LIKE '%%') OR (author LIKE '%%') OR (isbn LIKE '%%') OR (publisher LIKE '%%') ORDER BY title ASC LIMIT $start_from, $limit";

So sql go look for any title (which originate from '%%' condition), any author or isbn or publisher.因此 sql go 查找任何标题(源自 '%%' 条件)、任何作者或 isbn 或出版商。 One simple solution is to check the length of $keyword and if it was zero ignore searching for it:一种简单的解决方案是检查 $keyword 的长度,如果它为零,则忽略搜索:

 ...}else if(isset($keyword) && count($keyword)!=0){
   $sql = "SELECT * FROM books where (title LIKE '%".$keyword."%') OR (author LIKE '%".$keyword."%') OR (isbn LIKE '%".$keyword."%') OR (publisher LIKE '%".$keyword."%') ORDER BY title ASC LIMIT $start_from, $limit";

}

You are also mentioning that when you are typing nothing, you still got the results, and since you have not posted your html form, I guess there is an issue issue with the assigning value of $keyword (if you post html form I can verify that).您还提到,当您什么都不输入时,您仍然得到结果,并且由于您还没有发布您的 html 表单,我想 $keyword 的分配值存在问题(如果您发布 html 表单我可以验证那)。 You can easily check if that is the case by printing the $keyword and see if it really have the value you expect it to.您可以通过打印 $keyword 轻松检查是否是这种情况,并查看它是否真的具有您期望的值。

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

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