I have a PHP script which I'm trying to use to generate search results from a db, with multiple search text inputs and a submit. The three inputs are Term, Keyword and Location. I have achieved the search but the validation logic i am finding a tad difficult. I broke the query into three parts so that when one of the inputs is empty it exempts that part of the query being added to the full query. But because of the OR operator when the first input is empty the rest fails.
A second eye will be helpful here. Please don't mark this question as too generic just have a look at the query and you will see the problem.
$term = mysqli_real_escape_string ($con , $_GET['term'] );
$location = mysqli_real_escape_string ($con , $_GET['location'] );
$keyword = mysqli_real_escape_string ($con , $_GET['keyword'] );
if(empty($term)) {
$term1 = "";
}else{$term1 = "job_title LIKE '%".$term."%'";}
if(empty($location)) {
$loc1 = "";
}else{$loc1 = "location LIKE '%".$location."%'";}
if(empty($keyword)) {
$key1 = "";
}else{$key1 = "OR description LIKE '%".$keyword."%'";}
$sql = "SELECT * FROM jobs WHERE ".$term1." ".$loc1." ".$key1." ";
$r_query = mysqli_query($con,$sql);
while ($joblist = mysqli_fetch_array($r_query)){
$now = date('Y-m-d',time());