Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.