Home > Net >  Creating advanced search with keyword using PHP
Creating advanced search with keyword using PHP

Time:12-13

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. 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?

CodePudding user response:

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 = "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. One simple solution is to check the length of $keyword and if it was zero ignore searching for it:

 ...}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). 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.

  • Related