• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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
0
carlng
Asked:
carlng
1 Solution
 
lozlozCommented:
check your $search_town and $search_postcode variables by printing them out somewhere in the script
0
 
TaintedGodCommented:
$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.
0
 
red010knightCommented:
Well this will likely make the code longer, but if the above solutions do not help you can try something like:

if(strlen($search_company)>0){
$result= mysql_query("SELECT * FROM $table WHERE company like '$search_company' ORDER BY company") OR die("No Records Matched!");
}elseif(strlen($search_town)>0){
$result= mysql_query("SELECT * FROM $table WHERE town like '$search_town' ORDER BY company") OR die("No Records Matched!");
}elseif(strlen($search_company)>0){
$result= mysql_query("SELECT * FROM $table WHERE postcode like '$search_postcode' ORDER BY company") OR die("No Records Matched!");
}

Which is extra code and makes things difficult but it simplifies your database call and makes things a little more straightforward in the script.

Good Luck and happy coding,
Red010Knight
0
 
y2kwackoCommented:
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
0
 
carlngAuthor Commented:


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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now