Jump to content

PHP Search help


PRodgers4284

Recommended Posts

I have a basic search facility using two drop down options, the search works fine but when i open the search script it displays all the records in the database, i dont want any records display until the submit button is selected. Can anyone help me with this:

 

My code is:

 

<!--Search Job form  -->                    
Job Search:<form method="post" action="">
Job Category:
<select name="jobcatergory">
<option value="Please Select">Please Select</option>
   <option value="Construction">Construction</option>
     <option value="Banking and Insurance">Banking and Insurance</option>
     <option value="Sales">Sales</option>
   <option value="Other">Other</option>
</select>  Job Location:
<select name="joblocation">
<option value="Please Select">Please Select</option>
     <option value="Co.Antrim">Co.Antrim</option>
       <option value="Co.Down">Co.Down</option>
     </select>         
<input type="submit" value="Find a Job" />
</form>

<?php  

$sql = "SELECT * FROM job"; 
$jobcat = mysql_real_escape_string(trim($_POST['jobcatergory'])); 
$jobloc = mysql_real_escape_string(trim($_POST['joblocation']));

if ($jobcat != '' && $jobloc != '') { 
$sql .= " WHERE jobcatergory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'";
} 
else if ($jobcat != '' && $jobloc == '') { 
  $sql .= " WHERE jobcatergory LIKE '%$jobcat%'"; 
} 
else if ($jobloc != '' && $jobcat == '') { 
  $sql .= " WHERE joblocation LIKE '%$jobloc%'"; 
} 

$query = mysql_query($sql) or die(mysql_error()); 

if(mysql_num_rows($query) > 0) 
{ 
while ($job = mysql_fetch_array($query)) 
{ 
    $jobtitle=$job["jobtitle"];  
    $jobcatergory=$job["jobcatergory"];  
   ?> 
    
<table border="1" width="51%" id="table1" bgcolor="#FFFFFF"> 
    <tr> 
        <td width="131"><font face="Verdana" size="2">Job Title</font></td> 
        <td width="131"><font face="Verdana" size="2">Job Catergory</font></td> 
        <td width="131"><font face="Verdana" size="2">Job Description</font></td> 
    </tr> 
    <tr> 
        <td width="131"><font face="Verdana" size="2"><?php echo $job["jobtitle"]; ?></font></td> 
        <td width="148"><font face="Verdana" size="2"><?php echo $job["jobcatergory"]; ?></font></td> 
        <td width="148"><font face="Verdana" size="2"><?php echo $job["description"]; ?></font></td> 
    </tr> 
</table> 
   
<?php 
  }  
  } 
  else 
  { 
  echo '<p>There are no search results with the search criteria you entered.</p>'; 
  } 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.