php - advanced search not giving me the result when it reaches the else if stmt
I have two text boxes keywords and location. when i search with keywords AND location it gives me the result but when i search only with location it does not.
$keywords = isset($_POST['keywords']) ? $_POST['keywords']:'';
$location = isset($_POST['location']) ? $_POST['location']:'';
if (isset($keywords)){
$search = "SELECT * FROM table1
WHERE table1 .field1 LIKE :keyword OR table1 .field2 LIKE :keyword ";
if(isset($location)){
$search .= "AND table1 .field5 LIKE :location";
}
}else if(isset($location)){
$search ="SELECT * FROM table1
WHERE jtable1 .field5 LIKE :location";
}
$keywords="%".$keywords."%";
$location="%".$location."%";
$statement = $connection->prepare($search);
$statement->execute(array(
':keyword'=> $keywords,
':location'=>$location
));
$result = $statement->fetchAll();
The first if stmt works but when when i search by location only, it gives me all the result but i just want to give result by that location.