merylvingien Posted October 14, 2009 Share Posted October 14, 2009 Hi guys, I have this code: if(isset($_POST['selected'])) { foreach($_POST['selected'] as $item) { $sql = "SELECT * FROM tablename WHERE ID=$item"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $result = mysql_query($sql); $num_rows = mysql_num_rows($result); if ($num_rows>6) { echo ("Too many checkboxes were selected"); } else while ($db_field = mysql_fetch_assoc($result)) { echo "stuff that is ok if number of checkboxes is correct"; } } } I am trying to limit the number of checkboxes that were selected, to say a max of 6. If the user selected any more than 6 a message should appear saying too many checkboxes were selected. Can anyone see where i went wrong? Cheers for any help fellas... Link to comment https://forums.phpfreaks.com/topic/177703-solved-limiting-checkboxes/ Share on other sites More sharing options...
ialsoagree Posted October 14, 2009 Share Posted October 14, 2009 Would be pretty helpful if we could see the HTML too. Link to comment https://forums.phpfreaks.com/topic/177703-solved-limiting-checkboxes/#findComment-936997 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 you should really do the error checking before the db query. assuming $_POST['selected'] is an array of checkboxes from a form... if (isset ($_POST['selected']) && (is_array ($_POST['selected']))) { if (count ($_POST['selected']) > 6) { echo ("Too many checkboxes were selected"); } else { //number of checkboxes ok .. do something; } } else { //form not submitted OR $_POST['selected'] is not an array; } Link to comment https://forums.phpfreaks.com/topic/177703-solved-limiting-checkboxes/#findComment-937000 Share on other sites More sharing options...
merylvingien Posted October 14, 2009 Author Share Posted October 14, 2009 Thank you kind sir, all working hunky dory! Link to comment https://forums.phpfreaks.com/topic/177703-solved-limiting-checkboxes/#findComment-937010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.