Solved

php multiple variable search

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
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 count occurrences of each item in an array.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now