Jump to content

multiple search parameters


gigido

Recommended Posts

hi i have 3 drop down fields, when a drop down is left blank (which i have given no value, so "") i seem to get an error on my search results. how do i make it so when there is no value in one of the fields that all values are retrieved for that particular drop down menu, so that its not searching for "" .

Link to comment
https://forums.phpfreaks.com/topic/96635-multiple-search-parameters/
Share on other sites

You should have some JavaScript on your form submit, i.e.

 

<form method="POST" action="do_something.php" onSubmit="return ValidateFields();">
...
</form>

 

Your ValidateFields() function will need to check all the fields contain valid values.  Then on your backend (do_something.php), you should also check that values submitted from the form are valid, i.e.

 

$val1 = $_POST["value1"];
$val2 = $_POST["value2"];
$val3 = $_POST["value3"];

if ( (!empty($val1)) && (!empty($val2)) && (!empty($val3)) ){
// it is valid
} else {
  die("Please fill in all form fields!);
}

hi, i dont see a need for java or validation. its a drop down menu, so its okay to have an empty value

<select name="state" id="state">
      <option value=""></option>
  <option value="ma">ma</option>
      <option value="ca">nh</option>
      <option value="nh">nh</option>
      <option value="vt">vt</option>
    </select>
number:
<select name="number" id="number">
    <option value="*"></option>
<option value="1">1</option>
  	<option value="2">2</option>
  	<option value="3">3</option>
  	<option value="4">4</option>
</select>
<input name="title" type="text" id="title" />
<input name="Search" type="submit" id="Search" value="search" />
</div>
  </label>
</form>

 

 

 

the php

$result = mysql_query ("SELECT * FROM listings WHERE state= ".$_POST['state']." and number =".$_POST['number]." and title = ".$_POST['title']."" );

 

 

so you have option to select a state, number from a drop down menu, as well as search via the text search .. so if you just want to search by state you select state from the drop down menu and the other values remain empty. so for my query to search the database i get an error if the values are empty. so how do a make it so when the drop down menu is empty that its not searching for an empty value in my database.

I would imagine that all you have to do is replace all those mySQL query AND statements to 'OR' because if someone leaves a field blank, it's returned as 'null'. So you're saying that that field in that row in the database MUST be null AND must have this title and this state etc.

 

You've also got loads of Syntax errors:

 

Replace:

$result = mysql_query ("SELECT * FROM listings WHERE state= ".$_POST['state']." and number =".$_POST['number]." and title = ".$_POST['title']."" );

 

With:

$result = mysql_query ("SELECT * FROM `listings` WHERE state= \".$_POST['state'].\" or number =\".$_POST['number].\" or title = \".$_POST['title'].\"" );

 

Because you are ending the query at every quotation! I was gonna just replace mysql_query("") with ('') but you have inverted commas in $_POST['state'] so I used a backslash (\) which avoids php from reading the quotation mark after it.

 

I've also added ` around your table name as this is better syntax for SQL.

 

Happy coding!

Dan.

there are no null values in my database, but are you saying when i us the "or" it will ignore any fields that are left blank? cause if it was "select from the table where title= 1 or number = 2"... I would only want the value returned that would have the same number and title.... not values that have 1 or the other.

No, what 'and' is doing is this:

 

Say the user left the state dropdown blank, the number field 2 and typed title as "bob":

 

OK troops! I want you to find the record WHERE the state = nothing, the number = "2" and title = "bob", KAPEESH!?

 

Now really, nothing is gonna turn up because NONE of the data in your table has a state value that is null.... See what I mean? They SHOULD have found what they were looking for but because the state HAS to be blank in order to meet the search criteria, nothing is returned.

 

Your Query should be this:

 

$result = mysql_query ("SELECT * FROM `listings` WHERE state= \".$_POST['state'].\" or number =\".$_POST['number].\" and title = \".$_POST['title'].\"" );

 

So now it's saying that the state can be this OR the number can be this, however BOTH have to match whatever row has that title. (or, and instead of and, and)

 

Also, if you want the title field to be like a real search e.g.:

 

The actual field is bob Marley

and the user has searched for 'bob'

 

You'd say:

 

SELECT * FROM `listings` WHERE state= ".$_POST['state']." or number =".$_POST['number']." and title LIKE ". $_POST['title'] ."

 

Regards,

Dan.

 

PS: Did it work?

makes perfect sence. thank you for clarifying.  umm... still getting an error of "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource"

 

heres what the code is:

$result = mysql_query ("SELECT * FROM `listings` WHERE number= ".$_POST['number']." or state =".$_POST['state']." and title LIKE ". $_POST['title'] ."");

while ($row = mysql_fetch_array($result)) {
$title = $row['title'] ;
echo "<center>title: <b>".$row['title']."</b></center>";   
  }

 

 

also changed it to just $row = mysql_fetch_array($result))  as opposed to the while statement, and also had an error.

You forgot about the " replaced with \" didn't you? Probably my fault actually.... when I was explaining:  ::)

 

$result = mysql_query("SELECT * FROM `listings` WHERE number= \"".$_POST['number']."\" or state =\"".$_POST['state']."\" and title LIKE \"". $_POST['title'] .'\"'");

This should help you debug. Replace the $result = mysql_query() line with this:

 

echo "SELECT * FROM `listings` WHERE number= \"".$_POST['number']."\" or state =\"".$_POST['state']."\" and title LIKE \"". $_POST['title'] ."\"";
$result = mysql_query("SELECT * FROM `listings` WHERE number= \"".$_POST['number']."\" or state =\"".$_POST['state']."\" and title LIKE \"". $_POST['title'] ."\"") or die(mysql_error());

 

The first line will echo the query that you're trying to do, the second line will echo the mysql error if there's something wrong with the query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.