wright67uk Posted January 27, 2013 Share Posted January 27, 2013 I am trying to use check boxes for the first time within a php programme. I aim to generate a list of user_id's with a checkbox next to each user. And then run a query based on which checkboxes the user ticks. If I tick a single box everything works fine. If I tick more than one box, all user_id's seemed to be selected. In the example below, I have ticked all three boxes which are user_id 5,6 and 7 I havent selected anything for user_id 1 User_id 6 is on my registration database but not on my SNAG database. Why is user_id 1 appearing in my results? <html> <?php ### CONNECTION ### if(isset($_POST['processForm'])) { echo $mystring .= implode(' OR ', $_POST['user']); ### To use in sql query ### echo "<hr>"; $query = "SELECT * FROM snag_score WHERE user_id = $mystring"; $data = mysql_query($query); while($row = mysql_fetch_array($data)) { echo $row['user_id']. " = " . $row['total_score']; echo "<br />"; } } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="processForm" value="1" /> <?php $sql = "SELECT * FROM registration"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo '<input type="checkbox" name="user[]" value= ' . $row['user_id'] . ' >'; echo $row['fname']; echo "<br />"; } ?> <input type="submit"> </form> <html/> Note: $mystring echo's as 5 OR 6 OR 7, but all user_ids are pulled from my database. (where did user_id 1 come from?) 5 OR 6 OR 7 1 = 5 1 = 8 1 = 12 5 = 39 5 = 43 5 = 36 1 = 8 5 = 10 5 = 50 1 = 10 5 = 30 7 = 72 7 = 66 7 = 69 7 = 74 ed cae frank Quote Link to comment https://forums.phpfreaks.com/topic/273706-multiple-check-boxes-and-odd-mysql-results/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 27, 2013 Share Posted January 27, 2013 You are logically OR'ing the values in your query statement. That creates a TRUE boolean value and all the rows having a user_id that equates to a TRUE (probably all of them) are matched. You would either need to create a query WHERE user_id = 5 OR user_id = 6, ... or use the IN() comparison - WHERE user_id IN(5,6,7) Quote Link to comment https://forums.phpfreaks.com/topic/273706-multiple-check-boxes-and-odd-mysql-results/#findComment-1408602 Share on other sites More sharing options...
Barand Posted January 27, 2013 Share Posted January 27, 2013 (edited) The way to do it is this $mystring .= implode(',', $_POST['user']); ### To use in sql query ### $query = "SELECT * FROM snag_score WHERE user_id IN ($mystring)"; Edited January 27, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273706-multiple-check-boxes-and-odd-mysql-results/#findComment-1408605 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.