dk4210 Posted April 29, 2011 Share Posted April 29, 2011 Hello Guys, I am trying to figure this out.. I have a page that has several check boxes that when checked the values ends up as an array Like this 1, 3, 9, 6, That's what it looks like in the database. I want to compare the values (numbers) submitted with the values in my database table called benefits Here is the function I wrote this function and I want to check the values of the submitted values but I am just not sure how to compare the values, especially with a comma in the array. $benefits2 = $_POST['benefits']; function check_benefits ($benefits2,$member_id,$description,$ip){ $queryb = mysql_query("SELECT * FROM benefits WHERE b_id = $benefits2"); while ($row = mysql_fetch_array($queryb)) { $benefit_list = $row['b_id']; } if (isset($benefits2)&&( $benefit_list!== $benefits2)) { Do something here! } } Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/235073-compare-an-array-with-values/ Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 Does the numbers 1, 3, 9, 6 correspond to a value in your b_id field? If they do then you can do this. I assume $_POST['benifits'] is an array of the ids. $IDs = implode(',', $_POST['benifits']); $query = "SELECT * FROM benefits WHERE b_id IN ($IDs)"; $result = mysql_query($query); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { // do what you want with the results. } } That will return all records with the b_id value of 1, 3, 9 and 6 Link to comment https://forums.phpfreaks.com/topic/235073-compare-an-array-with-values/#findComment-1208139 Share on other sites More sharing options...
dk4210 Posted April 29, 2011 Author Share Posted April 29, 2011 Thanks for the quick response. I am trying to compare what is submitted form the form with whats in the database b_id column. I am developing a security system that basically looks at what has been submitted and compares it to whats in the database.. Will your code still work for that? ] Thanks! Link to comment https://forums.phpfreaks.com/topic/235073-compare-an-array-with-values/#findComment-1208144 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 My code selects the rows that correspond to the numbers within your $_POST['benifits'] array. So if you select the checkboxes that have the value of 1, 3 and 6. It'll return the records that have a b_id value of 1, 3 and 6 from your database. Link to comment https://forums.phpfreaks.com/topic/235073-compare-an-array-with-values/#findComment-1208151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.