Jump to content

Compare an array with values


dk4210

Recommended Posts

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

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

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.