Jump to content

PHP/MySQL Search w/ blank fields


r00tk1LL

Recommended Posts

I have a search form that has 5 fields, 1 drop-down box, & 1 set of radio buttons. I would like users to be able to submit any combination of fields and have only the filled in fields queried in the DB (MySQL).

Problem one:

How do I have the php file I'm posting to see that a field is empty, and then query only what has been submitted?

Problem two:

How do I use a wildcard function I.E. "LIKE" with these queries?

Here is some code

 

$username = "UN";
$password = "PW";
$hostname = "HN";
$db = "DB";
$link = mysql_connect ( $hostname, $username, $password);
//user input variables
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$marital=$_POST['marital'];
$hobbies=$_POST['hobbies'];
$interests=$_POST['interests'];
$sex = $_POST['sex'];
//start SQL connect
if (! $link){
die ("<center><h1>Couldn't connect to MySql Server</h1></center>");
}
mysql_select_db ($db) or die ("Couldnt find the database");
//process the query

//following line is the problem. Dont know how to use LIKE. Dont know if I should use || or &&
$query="SELECT field1, field2, field3 FROM mypages WHERE sex='$sex' && marital='$test' "; //would have more variables

$result=mysql_query($query); 

mysql_close($link);

 

I appreciate any help you can give me, here is the url if interested.

http://www.mynorthtexas.com/mypages/final/

Link to comment
https://forums.phpfreaks.com/topic/60445-phpmysql-search-w-blank-fields/
Share on other sites

  • 2 years later...

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.