Jump to content

[SOLVED] Search form with blank fields - how do I get it to return results?


waitingtoderail

Recommended Posts

Please excuse me if this is a completely obvious answer, I'm a complete php newbie in a database management class. I've been searching message boards and going through Peachpit Press manuals all day and can't make heads or tails of how to do it.

 

Here's my situation:

 

I'm trying to create a search form with four variables that will return results no matter how many of the fields are filled in. For example, a state field and a city field. If the searcher only fills in state, I want it to come back with all the cities listed in the database for that state.

 

So the search form and php form I created work, but only if I fill in all fields. Here's the code for the php form, can anyone help me out?

 

Baby steps...

 

$sql="select farm_name, farm_address, farm_city, farm_state, farm_zip, 

farm_description from farm_farm, farm_type, farm_product, farm_farmproduct 

where farm_product.product_id=farm_farmproduct.product_id and 

farm_farm.farm_id=farm_farmproduct.farm_id and 

farm_farm.type_id=farm_type.type_id and (farm_city like \"$_POST[city]\") and 

(farm_state like \"$_POST[state]\") and (farm_farm.type_id like 

\"$_POST[farmtype]\") and (farm_product.product_name like 

\"$_POST[product]\")" ;


$result=mysql_query($sql);

if($result==0)
echo ("<p>Error".mysql_errno()." is ".mysql_error()."</p>");

else
{
   echo("<table border=\"5\">");
   for($i=0;$i<mysql_num_rows($result);$i++)
   {
      echo("<tr>");
      $row_array=mysql_fetch_row($result);
      for($j=0;$j<mysql_num_fields($result);$j++)
        {
         echo("<td>".$row_array[$j]."</td>");
         }
       echo("</tr>");
    }
echo("</table>");
}
?>

Link to comment
Share on other sites

$sql="select farm_name, farm_address, farm_city, farm_state, farm_zip,

farm_description from farm_farm, farm_type, farm_product, farm_farmproduct

where farm_product.product_id=farm_farmproduct.product_id and

farm_farm.farm_id=farm_farmproduct.farm_id and

farm_farm.type_id=farm_type.type_id and ((farm_city like \"$_POST[city]\") or

(farm_state like \"$_POST[state]\") or (farm_farm.type_id like

\"$_POST[farmtype]\") or (farm_product.product_name like

\"$_POST[product]\"))" ;

Link to comment
Share on other sites

I may be missunderstanding the problem, But i would do this:

 

I would build the SQL select on the fly based on the submitted info.

 

Ie, only select for stuff that has been submitted.

 

So if i would search for age sex location, but only filled in age location.

 

id only do a select * from foo where age = bar and location = bar;

 

if i filled all in , then my statement would be

select * from foo where age = bar and sex = bar and location = bar;

 

if that made sense?

 

Probably not the best way to do it, but thats how i would :)

 

Link to comment
Share on other sites

$sql="select farm_name, farm_address, farm_city, farm_state, farm_zip,

farm_description from farm_farm, farm_type, farm_product, farm_farmproduct

where farm_product.product_id=farm_farmproduct.product_id and

farm_farm.farm_id=farm_farmproduct.farm_id and

farm_farm.type_id=farm_type.type_id and ((farm_city like \"$_POST[city]\") or

(farm_state like \"$_POST[state]\") or (farm_farm.type_id like

\"$_POST[farmtype]\") or (farm_product.product_name like

\"$_POST[product]\"))" ;

 

This worked, but if I search on more than one field, it gives me ALL the results for each field search. What I need is, if I search for "RI" in state, and "Goats" in product, it will only return farms that sell goats in Rhode Island. As it stands it returns all farms in RI and all farms that sell goats.

Link to comment
Share on other sites

I may be missunderstanding the problem, But i would do this:

 

I would build the SQL select on the fly based on the submitted info.

 

Ie, only select for stuff that has been submitted.

 

So if i would search for age sex location, but only filled in age location.

 

id only do a select * from foo where age = bar and location = bar;

 

if i filled all in , then my statement would be

select * from foo where age = bar and sex = bar and location = bar;

 

if that made sense?

 

Probably not the best way to do it, but thats how i would :)

 

This sounds like what I need but is so far over my newbie head that I don't get it. What does the * represent? Foo is the database name?

Link to comment
Share on other sites

okay then you are going to have to go back to using AND instead of OR but only querying for the ones selected (what lynxus said) for example:

 

$sql="select farm_name, farm_address, farm_city, farm_state, farm_zip,
farm_description from farm_farm, farm_type, farm_product, farm_farmproduct
where farm_product.product_id=farm_farmproduct.product_id and
farm_farm.farm_id=farm_farmproduct.farm_id and
farm_farm.type_id=farm_type.type_id";

$sql .= (trim($_POST['city']) != "")? " and (farm_city like \"$_POST[city]\")" : "";
$sql .= (trim($_POST['state']) != "")? " and (farm_state like \"$_POST[state]\")" : "";
$sql .= (trim($_POST['farmtype']) != "")? " and (farm_farm.type_id like \"$_POST[farmtype]\")" : ""; 
$sql .= (trim($_POST['product']) != "")? " and (farm_product.product_name like \"$_POST[product]\")" : "";

Link to comment
Share on other sites

okay then you are going to have to go back to using AND instead of OR but only querying for the ones selected (what lynxus said) for example:

 

$sql="select farm_name, farm_address, farm_city, farm_state, farm_zip,
farm_description from farm_farm, farm_type, farm_product, farm_farmproduct
where farm_product.product_id=farm_farmproduct.product_id and
farm_farm.farm_id=farm_farmproduct.farm_id and
farm_farm.type_id=farm_type.type_id";

$sql .= (trim($_POST['city']) != "")? " and (farm_city like \"$_POST[city]\")" : "";
$sql .= (trim($_POST['state']) != "")? " and (farm_state like \"$_POST[state]\")" : "";
$sql .= (trim($_POST['farmtype']) != "")? " and (farm_farm.type_id like \"$_POST[farmtype]\")" : ""; 
$sql .= (trim($_POST['product']) != "")? " and (farm_product.product_name like \"$_POST[product]\")" : "";

 

Thanks so much for your help here, this worked, but is again giving me the same results multiple times. It's strange though because it's not giving me nearly as many repetitions and not all of them repeat.

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.