Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading