php - advanced search not giving me the result when it reaches the else if stmt

989

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.

People are also looking for solutions to the problem: php - how to duplicate the code $ _GET on page 1 to page 2 automatically using the button click

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.