PHP Variable with Array
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) . ")";
// execut开发者_运维问答e the query (...)
}
include('../closeconn.php');
?>
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 (...)
}
精彩评论