Hi

This is probably a fairly easily solved issue but I am struggling at the moment. I basically just want to be able to search a database but with several different options, any of which can be used and any combination can be used. The options will be name, date and location.

What is the best way to do the search, I only want to search if something is entered into the form for that part so if no name is entered I do not want it to search for that etc.

Another issue is the date and location are drop down menus, how do I use if(isset) for drop down menus, do I leave a black <select> option?

Thanks in advance

Member Avatar for diafol

isset should be fine.

if(isset($_POST['name']))$fields[] = "`name` = '". mysql_real_escape_string($_POST['name'])  ."'";

if(isset($_POST['date']))$fields[] = "`date` = '". mysql_real_escape_string($_POST['date'])  ."'";

if(isset($_POST['location']))$fields[] = "`location` = '". mysql_real_escape_string($_POST['location'])  ."'";

if(isset($fields)){
  $pairs = " WHERE " . explode(',',$fields) : '';
}else{
  $pairs = "";
}
$query = "SELECT field1 FROM table1" . $pairs; 

That will return all data if no parameters entered. If you don't want that, then you can modify to this:

if(isset($fields)){
  $query = "SELECT field1 FROM table1 WHERE " . explode(',',$fields);
  ...
}else{
  echo "Apply a search item";
}

I should note that there are better ways (and shorter ways) of doing this. But I thought this would be the easiest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.