Hi, I am trying to query a MySql database based on which search fields are filled in and ignoring those that aren't. I have only got as far as the first part and it isn't working, could anyone tell me why? if ((isset($_POST['band'])) && (!isset($_POST['day'])) && (!isset($_POST['month'])) && (!isset($_POST['year'])) && (!isset($_POST['location']))) { $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%$band%'"); while($row = mysql_fetch_array($result)) { $page = sprintf( "<a href='http://www.example.com/%s'>%s</a>" , $row['PageName'] , $row['band'] ) ; echo 'Band Name: '; echo $row['band'] ." " . " " . $row['day'] . " " . $row['month'] . " " . $row['year']. " " . $row['Genre']; echo $page; echo "<br />"; } } else echo 'No gigs found' ; PHP: Thanks in advance
Try these: 1.] Make sure that your if condition is actually retrieving "true", having a long and multiple "&&" and "||" might be confusing. 2.] Try changing $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%$band%'"); PHP: to $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%{$band}%'"); PHP:
Hi. It seems to me that your code must be changed to: $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%".$_POST['band']."%'"); PHP: If my reply helps you add to my reputation, please.
Thanks for the responses, with your help and using empty() I have got it sorted, now I have a different problem though. If I search for data that is in database of one of the conditions it doesn't find anything, I think the problem is to with this part: $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%".$_POST['band']."%' AND day='%".$_POST['day']."%' AND month='%".$_POST['month']."%' AND year='%".$_POST['year']."%'"); PHP: The day and year are numerical and month is text. I have tried removing ' ' from the numerical ones but that did work, or I have done it wrong. Any ideas? Thanks
Hi, First assign your $_POST values to variables so you can get cleaner code. i also don't know why you add % on day, month and year. Aren't you supposed to get the exact values on those fields? Try using these code. $band_name = $_POST['band']; $gig_day = $_POST['day']; $gig_month = $_POST['month']; $gig_year = $_POST['year']; $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%{$band_name}%' AND day='{$gig_day}' AND month='{$gig_month}' AND year='$gig_year'"); PHP: Also be sure the the three condition, $gig_day AND $gig_month AND $gig_year, is true. Check your database, if such column do exist.