Jump to content

Recommended Posts

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

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)

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 by Barand
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.