Solved

php multiple variable search

Posted on 2003-10-30
5
265 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
[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
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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