PHP复选框和操作

时间:2010-11-03 01:54:03

标签: php mysql checkboxlist

我有以下代码,允许管理员通过从可用角色(复选框)中进行选择,将特定用户添加到多个角色。代码可以很好地完成此任务,但它还需要删除从中取消选择角色(复选框)的用户。复选框允许管理员在所选角色中添加和删除用户。这是我无法弄清楚的部分。

如果有人能引导我完成这一点,那将是非常棒的。谢谢!

<?php
// declare variables
$msg = '';

// ------------------------------------------------------------------
// UPDATE USER INFO
// ------------------------------------------------------------------
if(isset($_POST['UpdateUser']))
{
    // get user id from query string sent
    $sent_id = mysqli_real_escape_string($conn, $_GET['uid']);
    if(isset($sent_id) && !empty($sent_id) && is_numeric($sent_id) && $sent_id > 0)
    {
        $user_id = $sent_id;

        // ------------------------------------------------------------------
        // ADD CURRENT USER TO SELECTED ROLES
        // ------------------------------------------------------------------
        if(isset($_POST['checked']))
        {
            // get selected role names
            $checked = $_POST['checked'];

            $i1=0;
            $i2=0;
            foreach($checked as $role_name)
            {
                // get role id
                $get_role_id = mysqli_query($conn, "SELECT RoleId FROM roles WHERE RoleName = '$role_name'")
                or die($dataaccess_error.mysqli_error($conn));

                $row = mysqli_fetch_array($get_role_id);
                $role_id = $row['RoleId'];

                // check if user already exist in role
                $check_if_exist = mysqli_query($conn, "SELECT UserId, RoleId, RoleName FROM users_in_roles WHERE UserId = $user_id AND RoleId = $role_id AND RoleName = '$role_name' LIMIT 1")
                or die($dataaccess_error);

                if(mysqli_num_rows($check_if_exist) == 0)
                {
                    // add user to roles
                    $add_user_to_roles = mysqli_query($conn, "INSERT INTO users_in_roles(UserId, RoleId, RoleName) VALUES($user_id, $role_id, '$role_name')")
                    or die($dataaccess_error.mysqli_error($conn));

                    $count1 = $i1++ + 1;
                    $count2 = $i2;
                    $msg = "<div class='msgBox3'>SUCCESS: USER have been ADDED to ($count1) ROLES - AND ALREADY EXISTS in ($count2).</div>";
                }
                elseif(mysqli_num_rows($check_if_exist) == 1)
                {
                    $count1 = $i1;
                    $count2 = $i2++ +1;
                    $msg = "<div class='msgBox4'>NOTE: USER have been ADDED to ($count1) ROLES - AND ALREADY EXISTS in ($count2).</div>";
                }
            }
        }
        else
        {
            $msg = $msg_error2;
        }
    }
}
?>

2 个答案:

答案 0 :(得分:0)

简单的解决方案是使用复选框以外的其他内容,因为在取消选中时它们不会提交值。

或者,您可以保留复选框并构建可能的角色值的PHP数组。在当前循环中,您可以在选中时删除这些值。在循环之后,数组将仅保留未经检查的角色,然后您可以相应地删除它们。

$roles = array('key' => 'Nice Name', 'admin' => 'Administrator');

这不仅可用于输出复选框,还可用于管理角色。理想情况下,这将在数据库表中。

答案 1 :(得分:0)

在从提交的复选框中创建新的用户/角色关系之前删除它们。如果将复选框值设置为role_id而不是role_name,则还可以在单​​个MySQL语句中插入所有角色。此外,users_in_roles表没有理由复制role_name列。这就是我使用的(当然是简化的伪语句):

// New data structure
mysqli_query("DROP TABLE users_in_roles");
mysqli_query("CREATE TABLE users_in_roles ( user_id ..., role_id ....)");

....

// Build SQL fragments for multi-row INSERT.
$a = array();
foreach($checked as $role_id) {
 $a[]= sprintf('(%d, %d)', $user_id, $role_id);
}
// Delete existing user/role relationships for the user.
mysqli_query("DELETE FROM users_in_roles WHERE user_id = $user_id");
// Insert multiple rows in a single statement.
mysqli_query(
 sprintf('INSERT INTO users_in_roles (user_id, role_id) VALUES %s', 
   implode(', ', $a)));

此方法完成相同的操作 - 仅使用两个sql语句。一个缺点:如果在INSERT语句之前/期间出现错误,则不会分配任何角色。您可以在事务中执行DELETEINSERT