KitCarl Posted April 26, 2010 Share Posted April 26, 2010 This is my first post so hello and I hope after reading some of the guidelines I get it right. I've got a limited but functional idea of how to have MYSQL use a variable in a query. Is it possible to have MYSQL check for mutliple values as in an array? Below is the query. I'm sure the code could be improved any any help appreciated, but what I really want to be able to do is have the variable $association, which now is created from id field from a radio button that only allows one choice, become a check box that allows multiple selections. How would I have the MYSQL statement written to check against multiple values in $association? Select TrialListing.listingID, BeagleClub.clubName AS Club, CONCAT_WS('-', DATE_FORMAT(TrialListing.dateBegin, ('%m/%e')), DATE_FORMAT(TrialListing.dateEnd, ('%m/%e'))) as D, TrialCategory.categoryDesc AS TrialType, CONCAT_WS(' on ', Format.formatLong, Game.game)as Format, CONCAT('$', TrialListing.entryCostID) AS Entry, Prize.prizeName AS Awards, TrialListing.notes AS Comments FROM TrialListing, BeagleClub, TrialCategory, Format, Game, Prize Where TrialListing.clubID = BeagleClub.clubID AND TrialListing.categoryID = TrialCategory.categoryID AND TrialListing.formatID = Format.formatID AND TrialListing.gameID = Game.gameID AND TrialListing.prizeID = Prize.prizeID AND TrialListing.dateEnd >= $date AND BeagleClub.clubAssociation = '$association' AND TrialListing.categoryID = '$trial_type' ORDER BY TrialListing.dateBegin, TrialListing.dateEnd Quote Link to comment https://forums.phpfreaks.com/topic/199838-can-where-check-for-multiple-values-passed-from-a-variable-or-array/ Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 If you wish it to be one of several values you can use syntax along the lines of... WHERE field IN (value1, value2, value3) If you create the checkboxes as an array (by naming them all 'something[]') you can easily convert the selected values into this format using... $association = "'" . implode("','", $_POST['something']) . "'"; So your query would then simply be... Select TrialListing.listingID, BeagleClub.clubName AS Club, CONCAT_WS('-', DATE_FORMAT(TrialListing.dateBegin, ('%m/%e')), DATE_FORMAT(TrialListing.dateEnd, ('%m/%e'))) as D, TrialCategory.categoryDesc AS TrialType, CONCAT_WS(' on ', Format.formatLong, Game.game)as Format, CONCAT('$', TrialListing.entryCostID) AS Entry, Prize.prizeName AS Awards, TrialListing.notes AS Comments FROM TrialListing, BeagleClub, TrialCategory, Format, Game, Prize Where TrialListing.clubID = BeagleClub.clubID AND TrialListing.categoryID = TrialCategory.categoryID AND TrialListing.formatID = Format.formatID AND TrialListing.gameID = Game.gameID AND TrialListing.prizeID = Prize.prizeID AND TrialListing.dateEnd >= $date AND BeagleClub.clubAssociation IN($association) AND TrialListing.categoryID = '$trial_type' ORDER BY TrialListing.dateBegin, TrialListing.dateEnd Quote Link to comment https://forums.phpfreaks.com/topic/199838-can-where-check-for-multiple-values-passed-from-a-variable-or-array/#findComment-1048965 Share on other sites More sharing options...
KitCarl Posted April 26, 2010 Author Share Posted April 26, 2010 Cags, Thank You very much , worked great! Now a secondary question created by the change. What do you do to prevent the MYSQL error if a user submits the form with all boxes unchecked? I tried the code below but obviously it is wrong. IF (!empty($_POST['association'])) $association = $association = "'" . implode("','", $_POST['association']) . "'"; ELSE $association = 'placed default value here'; Quote Link to comment https://forums.phpfreaks.com/topic/199838-can-where-check-for-multiple-values-passed-from-a-variable-or-array/#findComment-1049015 Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 The code you have is essentially correct, the one thing you are missing is that the string must be surrounded by quotes within the query, so therefore you should probably use "'placed default value here'". Alternatively you could remove the "'" sections from the original code and place them either side of $association within the query. Quote Link to comment https://forums.phpfreaks.com/topic/199838-can-where-check-for-multiple-values-passed-from-a-variable-or-array/#findComment-1049020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.