Solved

PHP Search facility

Posted on 2009-04-01
10
275 Views
Last Modified: 2012-05-06
I've setup a simple search facility to search for jobs. I have dropdowns for Job, Location, Type and Salary.

The salary dropdown is where I'm having issues, i have it as follows:

<select name="jobSalary" id="jobSalary">
            <option value="" selected >Select...</option>
            <option value="10,000" >Up to £10,000</option>
            <option value="10,000-11,000" >£10,000 - £11,000</option>
            <option value="12,000-13,000" >£12,000 - £13,000</option>
            <option value="14,000-15,000" >£14,000 - £15,000</option>
            <option value="16,000-17,000" >£16,000 - £17,000</option>
            <option value="18,000-19,000" >£18,000 - £19,000</option>
            <option value="20,000-21,000" >£20,000 - £21,000</option>
            <option value="22,000-23,000" >£22,000 - £23,000</option>
            <option value="24,000-25,000" >£24,000 - £25,000</option>
            <option value="25,000-30,000" >£25,000 - £30,000</option>
            <option value="30,000-34,000" selected="selected">£30,000 - £34,000</option>
            <option value="35,000-39,000" >£35,000 - £39,000</option>
            <option value="40,000-44,000" >£40,000 - £44,000</option>
            <option value="45,000" >£45,000 +</option>
          </select>

Whatever I select no jobs are displayed, my sql is as follows, I first split the jobSalary value into 2 so I can search between values:

$salary = $_POST["jobSalary"];
list($from, $to) = split('-', $salary);
       
mysql_select_db($database_mindset, $mindset);
$query_RSvac = "SELECT mind_client_jobs.*, mind_client.*\n"
    . "FROM mind_client_jobs LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID WHERE mind_client.clientActive = 'Y' AND mind_client_jobs.jobActive = 'Y' AND mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%' AND mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to' ORDER BY jobDate DESC";
$RSvac = mysql_query($query_RSvac, $mindset) or die(mysql_error());
$row_RSvac = mysql_fetch_assoc($RSvac);
$totalRows_RSvac = mysql_num_rows($RSvac);

I think it must be a problem with the salary as once I take this out of the sql query it works using the other fields.

Thanks

Chris
0
Comment
Question by:djfenom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:pkoops
ID: 24037148
I don't know if the "," might be a problem.
Try using 30000-34000 and split this.
You maybe need to alter your database then, but this might work better, if you add the "," later on, as you are showing the values.
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24037173
Hello,

Might be because of :

change after this line " list($from, $to) = split('-', $salary); " :

$from = ''.$from.'';
$to = ''.$to.'';

And try again.

Regards.
0
 

Author Comment

by:djfenom
ID: 24037206
Thanks guys, I've tried your suggestion shobinsun but it makes no difference.

I can actually write $from and $to to the page no problem, it just won't work in the SQL.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24037223
Two things to check/try:

1. You are using $_POST, make sure your form has method='post' in it

2. Take the commas out of the value clauses like so. Rather than

<option value="20,000-21,000" >£20,000 - £21,000</option>

try

<option value="20000-21000" >£20,000 - £21,000</option>

because the query will look like mind_client_jobs.jobSalaryTo BETWEEN '30,000' AND '34,000'  and MySQL does not store commas in integer or float fields. I am assuming that these fields are numeric, but that is where I would start.
0
 

Author Comment

by:djfenom
ID: 24037289
Hi bportlock, I've checked and it's definitely using method="post".

I've also taken out the comma in the values, but still the same? My field is set to INT in my database.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24037362
In that case, perhaps your quesy is selecting on exclusive conditions. For example a query to pick people called "Mary" might return some records, but one to pick someone called "Mary" and sex='Male' will return no records. I have restructured the query as follows

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobActive = 'Y' AND
                          mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND
                          mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND
                          mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%' AND
                          mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to'
                    ORDER BY jobDate DESC";

take all the WHERE conditions out and try adding them back one at a time and see if the query starts returning no records. With this

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    ORDER BY jobDate DESC";


then the BETWEEN stuff

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE
                          mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to'
                    ORDER BY jobDate DESC";


add another condition...

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to'
                    ORDER BY jobDate DESC";

and so forth.
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24037380
did you try to issue an "echo" of the actual query to see what's the exact string?
0
 

Author Comment

by:djfenom
ID: 24037453
I've had a play with changing the WHERE conditions and as soon as I take out the salary one, it works fine:

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobActive = 'Y' AND
                          mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND
                          mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND
                          mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%'
                    ORDER BY jobDate DESC";

I've done an echo of the query and done a search just on the salary and this is what I got:

SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID WHERE mind_client.clientActive = 'Y' AND mind_client_jobs.jobActive = 'Y' AND mind_client_jobs.jobSector LIKE '%%' AND mind_client_jobs.jobCounty LIKE '%%' AND mind_client_jobs.jobType LIKE '%%' AND mind_client_jobs.jobSalaryTo BETWEEN '30000' AND '34000' ORDER BY jobDate DESC
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24037475
Have a look in your MySQL table. How many records are in there with a salary between 30000 and 34000? Can you dump some of those records and post them here?
0
 

Author Comment

by:djfenom
ID: 24037716
I've had a play and fixed it, I basically had to make sure that a salary was selected before the query was ran, here is my working version.

Thanks for your help
$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobActive = 'Y' AND
                          mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND
                          mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND
                          mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%'";
						  if ($_POST["jobSalary"] == "") {
						  $query_RSvac = $query_RSvac . " ORDER BY jobDate DESC";
						  }
						  else {
						 	 if ($from == "10000" && $to == "") {
							  $query_RSvac = $query_RSvac . " 
							  AND mind_client_jobs.jobSalaryTo <= '$from' ORDER BY jobDate DESC";
							 }
							 else if ($from == "45000" && $to == "") {
							  $query_RSvac = $query_RSvac . " 
							  AND mind_client_jobs.jobSalaryTo >= '$from' ORDER BY jobDate DESC";
							 }
							 else {
							  $query_RSvac = $query_RSvac . " 
							  AND mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to' ORDER BY jobDate DESC";
							 }
						  }

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question