2

I have this music site where i have a search sidebar where i want people to be able to find the right musician profile from my table. The search form is built with checkboxes and hooked up to my table with PHP and MySQL. I want people to find profiles from categories like location, user type, genre of music they play and if they have an album release.

<form action="udforsk.php"  method="post" enctype="multipart/form-data">
  <h2>User type</h2>
  <input type="checkbox" name="type[]" value="Solo">
  <input type="checkbox" name="type[]" value="Duo">
  <input type="checkbox" name="type[]" value="Band">

  <h2>Location</h2>
  <input type="checkbox" name="location[]" value="area_1">
  <input type="checkbox" name="location[]" value="area_2">
  <input type="checkbox" name="location[]" value="area_3">
  <input type="checkbox" name="location[]" value="area_4">
  <input type="checkbox" name="location[]" value="area_5">

  <h2>Genre</h2>
  <input type="checkbox" name="genre[]" value="genre_1">
  <input type="checkbox" name="genre[]" value="genre_2">
  <input type="checkbox" name="genre[]" value="genre_3">
  <input type="checkbox" name="genre[]" value="genre_4">
  <input type="checkbox" name="genre[]" value="genre_5">

  <h2>Album or demo</h2>
  <input type="checkbox" name="release[]" value="album">
  <input type="checkbox" name="release[]" value="demo">
</form>

I want this to be hooked up width a query, but i have problems how to deal with searches where some of the categories haven't been filled out. Like if none of the locations checkboxes is checked all locations is fair game in the query search.

Right now i'm trying to use IN to search after users my query looks like this but it gives me trouble when the $_POST input is empty. Right now i'm just trying to make it work with locations and genre.

$sql = "SELECT artist.ArtistID, artist.ArtistName, artist.RegionID, 
artist_genre.ArtistID, artist_genre.GenreID
FROM artist, artist_genre
WHERE artist.RegionID IN ($areaer) AND artist_genre.GenreID IN ($genrer)";

All help how query searches with multiple search variables with checkboxes in general would be alot of help.

Hazelcraft
  • 113
  • 8
  • [PHP: check if any posted vars are empty](https://stackoverflow.com/questions/3190464/php-check-if-any-posted-vars-are-empty-form-all-fields-required) – Masivuye Cokile Oct 10 '17 at 11:04
  • [Check whether $_POST-value is empty](https://stackoverflow.com/questions/9154719/check-whether-post-value-is-empty) – Masivuye Cokile Oct 10 '17 at 11:04
  • if you want to have a default value you could use a input type="hidden" so if nothing is ticked it will just post a default value – Sean Konig Oct 10 '17 at 11:05
  • [php- heck if variable is no empty before running query](https://stackoverflow.com/questions/14044774/php-check-if-variable-is-not-empty-before-running-insert-query) – Masivuye Cokile Oct 10 '17 at 11:06

1 Answers1

0

you can try this code:

$conditions = []; 
$query = 'your query ';
$conditions[] = isset($_POST['location']) ? 'artist.RegionID in ('.implode(',',$_POST['location']).')';
$conditions[] = isset($_POST['genre']) ? 'artist.GenreID in ('.implode(',',$_POST['genre']).')';
if(sizeof($conditions) > 0 ) query.='where '.implode(' and ',conditions);

but it's better if you work with prepared statement.