Solved

php multiple variable search

Posted on 2003-10-30
5
249 Views
Last Modified: 2006-11-17
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
Comment
Question by:carlng
5 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9654696
check your $search_town and $search_postcode variables by printing them out somewhere in the script
0
 
LVL 2

Expert Comment

by:TaintedGod
ID: 9654734
$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
 
LVL 3

Accepted Solution

by:
red010knight earned 125 total points
ID: 9655439
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
 
LVL 3

Expert Comment

by:y2kwacko
ID: 9655769
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
 

Author Comment

by:carlng
ID: 9656572


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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 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