Link to home
Start Free TrialLog in
Avatar of carlng
carlng

asked on

php multiple variable search

Hi

I am new to PHP programing and have a problem which is making me run around in circles.

basically I have 3 fields that the user can input to find a company by name, town or postcode or even combined field search.

heres my code

$result= mysql_query("SELECT * FROM $table WHERE company like '$search_company%' OR town = '$search_town' OR postcode = '$search_postcode' ORDER BY company") OR die("No Records Matched!");

when I want to do a search using the company field, it displays all records to the nearest match of my input. (this is fine), but when I want to do a search in the town or postcode field, it displays all the records in my table and doesn't perform the same search as for the company field.
is there something missing from my code?

Thks in advance

Carl
Avatar of lozloz
lozloz

check your $search_town and $search_postcode variables by printing them out somewhere in the script
$result= mysql_query("SELECT * FROM $table WHERE company like '$search_company%' OR town = '$search_town%' OR postcode = '$search_postcode%' ORDER BY company") OR die("No Records Matched!");

What i did there was added a % after each variable, ive seen other search engines and they all have the % sign after each variable, i am probably wrong, just a guess but give it a try.
ASKER CERTIFIED SOLUTION
Avatar of red010knight
red010knight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tainted, the % is a wildcard search so birch% would find both birchwood and birchhill, etc.  This is of course not necessary unless he wanted to search for partial matches.

red010, Although he does want to check the variables to see if they exist he also wants a combined search which is something your code does not do.  The following code should do it.

Begin code
------------------------------------------------------------------------------
$searchquery = "SELECT * FROM $table WHERE 1";

if (isset($search_company))
{
   $searchquery .= " AND `company` LIKE '%$search_town%'";
}

if (isset($search_town))
{
   $searchquery .= " AND `town`='$search_town'";
}

if (isset($search_postcode))
{
   $searchquery .= " AND `postcode`='$search_postcode'";
}

$searchquery .= " ORDER BY company";

$searchresults = mysql_query($searchquery);
---------------------------------------------------------------------------
End code


Since he had used the LIKE expression for company I figured he did want partial matching for company and exact matching for town and postcode.  I had added the WHERE 1 so that I could use AND within all additions without having to worry about whether I had added a previous condition or not.

Regards,
Kevin
Avatar of carlng

ASKER



I didn't realise I would get a response so quickly, thank you so much for your help,
A problem shared is a problem halved.
I've tried the code on my script and it worked first time, I just had to change the last variable from search_company to search_postcode,

thks again

rgds
Carl