I've created a page to display all users concerning or below a specific manager. If a user has registered, his account will be deactivated until the person concern activate it. i've displayed all the users and a checkbox whether to deactivate or activate. on submiting, the insert command is not working and nothing is being altered in the database. i've got the userid which is hidden but cant apply it. can you help me please
<?php
//get userid from login session
$memberid = $_SESSION['SESS_MEMBER_ID'];
//display all users concerning the user(login) is leader
$sql = mysql_query("SELECT * FROM user 开发者_Go百科WHERE userid in (SELECT userid FROM dept_user WHERE lead_id = '$memberid')");
//display results in table
print '<table border=1>';
print '<tr><td>Activate</td><td>First Name</td><td>Last Name</td><td>Email</td>td>Activate</td>';
print '<form method="GET" action="'.$PHP_SELF.'">';
$count = 0;
while($res = mysql_fetch_array($sql)){
$activate = $res['activate'];
$checked = '';
$user = $res['userid'];
if ($activate=='1'){
$checked = 'checked';
}
print '<tr><td><input type="checkbox" value="'.$user.'" name="activate[]" '.$checked.'></td>';
print '<td>'.$res['firstname'].'</td>';
print '<td>'.$res['lastname'].'</td>';
print '<td>'.$res['email'].'</td>';
print '<td>'.$res['activate'].'</td></tr>';
print '<input type="hidden" name="userid" value="'.$user.'">';
$count++;
}
print '</table>';
print '<br />With selected do:<br />';
print '<select name="useridselect">';
print '<option value="Select">Select</option>';
print '<option value="1">Activate</option>';
print '<option value="0">Desactivate</option>';
print '</select>';
print '<br /><input type="submit" value="Submit" name="submit">';
print '</form>';
$useridselect = $_GET['useridselect'];
if(isset($_GET['submit'])){
foreach($_GET['activate'] as $value){
print_r($value);
$update=mysql_query("UPDATE user SET activate='$useridselect' WHERE userid='$user'");
}
for($i=0;$i<$count;$i++){
$sql1="UPDATE user SET activate='$useridselect' WHERE userid='$user'";
$result1=mysql_query($sql1);
}
} else{
echo 'No actions yet performed';
}
?>
I think your update query is getting failed as you are not assigning checkbox value:
foreach($_GET['activate'] as $value){ print_r($value); //Here instead of userid='$user' should be userid='$value' $update=mysql_query("UPDATE user SET activate='$useridselect' WHERE userid='$value'"); }
I don't understand the purpose of the code below.
for($i=0;$i<$count;$i++){ $sql1="UPDATE user SET activate='$useridselect' WHERE userid='$user'"; $result1=mysql_query($sql1); }
Note: Please make sure that at least one of the checkbox is checked.
Change your GET method to a POST method, so the hidden values get POSTED as well to the script. Also, it's easier to seperate your DB logic instead of referring the same script over and over again.
You can follow the same logic for delete too. Use a checkbox array with value of userid
精彩评论