Jump to content

Searching Question


Accurax

Recommended Posts

Hi guys, another question im afraid,

I have a form that allows people to search my database.

The form has 20 fields that could potentially be selected, this information is then POSTed to a search results page which assigns the information to php variables and then queries the database.

Now, my querie uses AND statements because id like the results to only return what the user selects and not OR's.

My problem is that if the user does not fully complete the search form, and therefore passes empty strings to the results page, the query has a hissy fit and returns no results.

While the code is in effect correct, as there are no entries in my database with empty fields, I would like to figure out a way to ignore empty strings in my query.

so, assume $_POST['name']; is empty and $_POST['pet']; is equal to polar bear

[code]
$name = $_POST['name'];
$pet = $_POST['pet'];

$user_query = "SELECT * FROM mytable WHERE name='$name' && pet='$pet' ";

$result = mysql_query($user_query)
or die ("no can do");
[/code]

The above query would search the database for rows where the name was equal to "" and the pet was equal to "polar bear"... and it would therefore return no results.

Is there anyway of catering for this without writing thousands of if statements & specific queries for every eventuality ??
Link to comment
Share on other sites

mysql does have a wildcard operator... %
[code]
$name = $_POST['name'];
$pet = $_POST['pet'];

$user_query = "SELECT * FROM mytable WHERE name='$name' && pet='%$pet%' ";

$result = mysql_query($user_query) or die ("no can do");
[/code]

that much being said... if $pet="bear" it'll find "polar bear", "bear", "grizzley bear", etc...
but if you $pet="polar bear" it'll find "polar bear", and not bear, or grizzley bear
Link to comment
Share on other sites

[code]
if(!empty($_POST['pet'])){
$name = $_POST['name'];
$pet = $_POST['pet'];
$user_query = "SELECT * FROM mytable WHERE name='$name' && pet='%$pet%' ";
$result = mysql_query($user_query) or die ("no can do");
}else echo 'You must enter a specific search.';
[/code]
Link to comment
Share on other sites

[quote author=taith link=topic=122137.msg503342#msg503342 date=1168627150]
[code]
if(!empty($_POST['pet'])){
$name = $_POST['name'];
$pet = $_POST['pet'];
$user_query = "SELECT * FROM mytable WHERE name='$name' && pet='%$pet%' ";
$result = mysql_query($user_query) or die ("no can do");
}else echo 'You must enter a specific search.';
[/code]
[/quote]

Thats kind of what i was thinking before.... but the real situatiion has 20+ fields in the search form .... wouldnt this way result in 1000's of different connitations of the if else statement needing to be written ?
Link to comment
Share on other sites

yup... thousands of if()elesif()'s... or 1 foreach()... ;-)

[code]
$name = $_POST['name'];
$pet = $_POST['pet'];
foreach($_POST as $k=>$v) if(!empty($_POST[$k]))$query.=" && `$k`='%$v%'";
if(empty($query)) die("You must enter some fields");

$user_query = "SELECT * FROM mytable WHERE name='$name' $query";
$result = mysql_query($user_query) or die ("no can do");
[/code]
or something to that effect :-)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.