0

i'm trying to make a "update user's power" page. It is something similar to those you can find in say, invisionfree forums.

I need it to generate a list of members with checkbox [done] Add an option for it [done]

What i don't know how to do is to update, to say, give all the selected users the selected power.

Then i went searching for something and found most of them uses array to do this, but i never found one that actually explains how it works.

The example i took (with my own modification) was this:

while($row = mysql_fetch_array($result))
{
echo    '<tr>'.$id[]=$rows['id'].'';
echo    '<td width="50px" align="center" class="TableFormCell"><input type="checkbox" name="option[]" /></td>';
echo    '<td width="170px" align="center" class="TableFormCell">'.$row['uname'].'</td>';
echo    '</tr>';
}

I'm not sure what exactly $id[]=$rows['id'] does

I know after the row, my option[] would become an array of option[1], option[2], option[3]

for what power should be given, i've got no problem with that but on how to update the database i'm got no clue...

for($i=0;$i<$count;$i++){
$sql1="UPDATE ninos SET power='$power' WHERE id='$option[$i]'";
$result1=mysql_query($sql1);
}

So Say i have 5 users, Aye, Bee, Cee, Dee, Eee with IDs of 1,2,3,4,5 how can i make it so that my script would run like

$sql1="UPDATE ninos SET power = '$power' Where id='1','2','3','4','5'";

Please help, thanks.

Update

for nuqqsa

Here's the selecting page

$result = mysql_query("SELECT * FROM ninos");
while($row = mysql_fetch_array($result))
  {
echo    '<tr>';
echo    '<td width="50px" align="center" class="TableFormCell"><input     type="checkbox" name="option[]" value="' . $row['id'] . '" /></td>';
echo    '<td width="170px" align="center" class="TableFormCell">'.$row['uname'].'</td>';
echo    '</tr>';
}
?>

Here's the updating page

<?php
include('../openconn.php');

$power = $_POST['power'];

$ids = array();
foreach($_POST['option'] as $id)
{
$ids[] = (int)$id;
}   

if(!empty($ids)) {
// if there's at least one selected user
$sql1 = "UPDATE ninos SET power = '$power' Where id IN (" . implode(',', $ids) . ")";
// execute the query (...)
}


include('../closeconn.php');
?>
1

1 Answer 1

1

First, this doesn't make much sense (the assignment doesn't output anything printable):

echo '<tr>'.$id[]=$rows['id'].'';

This does the same yet it's clearer (the first line, since in a loop, will go on storing all the ids in an array):

$id[] = $rows['id'];
echo '<tr>';

UPDATE: Anyways there's no need to store the ids in an array here, just use $row['id'] to print the user identifier in the option value (right now no value is set):

while($row = mysql_fetch_array($result))
{
echo    '<tr>';
echo    '<td width="50px" align="center" class="TableFormCell"><input type="checkbox" name="option[]" value="' . $row['id'] . '"/></td>';
echo    '<td width="170px" align="center" class="TableFormCell">'.$row['uname'].'</td>';
echo    '</tr>';
}

The form action script will receive a $_POST['option'] variable containing the selected ids. An example of how it can be used:

$ids = array();
// input filtering: convert all values to integers
foreach($_POST['option'] as $id)
{
    $ids[] = (int)$id;
}    
if(!empty($ids)) {
    // if there's at least one selected user
    $sql1 = "UPDATE ninos SET power = '$power' Where id IN (" . implode(',', $ids) . ")";
    // execute the query (...)
}
Sign up to request clarification or add additional context in comments.

6 Comments

After submitting ,this is how i get the ID right? $id = $_POST['id']; then i have this as you stated $sql1="UPDATE ninos SET power='$power' WHERE id IN (".implode(',', $id).")"; but i've got this Warning: implode() [function.implode]: Invalid arguments passed
The second parameter of implode must be an array. This code assumes that $id is an array containing the user identifiers, NOT a $_POST variable. The array is generated through the instruction $id[] = $rows['id']; within the loop that iterates the query results.
Oh, I misunderstood your question, now I get it also includes the post part. Let me update the answer.
Thanks for the reply, but it doesn't seem to work. Do you have a working example i could have a look at :) ?
I've updated my post with my full script so you probably can have a better view. I've no idea what's wrong with it, it goes through everything but when i check my records, nothing is changed :(.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.