根据复选框编辑中间表

时间:2014-04-08 23:58:56

标签: php mysql

我的目标:

  

要更改我的MySql表,如下所示:

role_permission:
role_id    permissions_id
2          1
1          3
1          4
1          5
1          2

基于HTML代码:

<div class="element">
    <label for="permissions">Permissions:</label>
    <input type="hidden" name="permissions[1]" value="false" checked="">
    <label>
        <input type="checkbox" name="permissions[1]" value="true" checked="">&nbsp;canPushAPK</label>
    <input type="hidden" name="permissions[2]" value="false">
    <label>
        <input type="checkbox" name="permissions[2]" value="true">&nbsp;canBeCool</label>
    <input type="hidden" name="permissions[3]" value="false" checked="">
    <label>
        <input type="checkbox" name="permissions[3]" value="true" checked="">&nbsp;canEditSettings</label>
    <input type="hidden" name="permissions[4]" value="false" checked="">
    <label>
        <input type="checkbox" name="permissions[4]" value="true" checked="">&nbsp;canManageRoles</label>
    <input type="hidden" name="permissions[5]" value="false" checked="">
    <label>
        <input type="checkbox" name="permissions[5]" value="true" checked="">&nbsp;canAddUser</label>
    <input type="hidden" name="permissions[6]" value="false">
    <label>
        <input type="checkbox" name="permissions[6]" value="true">&nbsp;canFoo</label>
    <input type="hidden" name="permissions[7]" value="false">
    <label>
        <input type="checkbox" name="permissions[7]" value="true">&nbsp;canTalk</label>
    <input type="hidden" name="permissions[8]" value="false">
    <label>
        <input type="checkbox" name="permissions[8]" value="true">&nbsp;canTest</label>
    <input type="hidden" name="permissions[9]" value="false">
    <label>
        <input type="checkbox" name="permissions[9]" value="true">&nbsp;canPoo</label>
</div>
<input type="hidden" name="roleId" value="1">

View as JSFiddle.

然后基于检查的值&#34; roleId&#34; = 1,它插入DB。对于所有未选中的内容,它会删除此行。

IE: 如果选中所有内容,它将如下所示:

role_permission:
role_id    permissions_id
2          1
1          1
1          2
1          3
1          4
1          5
1          6
1          7
1          8
1          9

我在想什么: 两个问题:

DELETE FROM role_permissions
WHERE role_id = ?

然后:

$query = $this->mysqli->prepare("INSERT INTO role_permission(role_id,permission_id) VALUES (?,?)");
        $query->bind_param("ii", $roleId, $permissionId);
        $roleId = $_POST['roleId'];
foreach ($_POST['permissions'] as $permissionId=> $value){
    if ($value){
        $query->execute();
    }
}

1 个答案:

答案 0 :(得分:0)

你的方法会奏效。你似乎喜欢HTML。

如果您使用'权限名称'数组以及是否有效,那么代码只是foreach循环,如下所示:

测试代码:PHP 5.3.18,windows xp上的mysqli(Oi,停止laffin')

<?php // Q22950444
// program smarter not harder...
// arrays are your friend...

$allPermissions =
        array('canPushAPK'  => false, "canBeCool" => false, "canEditSettings" => false,
              "canManageRoles" => false, "canAddUser" => false, "canFoo" => false,
              "canTalk" => false, "canTest" => false, "canPoo" => false);

$mysqlhost   = 'localhost';
$mysqluser   = 'test';
$mysqlpass   = 'test';
$myDBname     = 'testmysql';
$mysqli = new mysqli($mysqlhost, $mysqluser, $mysqlpass, $myDBname);


if (!empty($_POST['goForIt'])) {
    if (!empty($_POST['permissions'])) {
        foreach($_POST['permissions'] as $permissionName) {
            if (isset($allPermissions[$permissionName])) { // test is valid permission
                $allPermissions[$permissionName] = true;
            }
        }
    }

    // process db...
    $sql = "delete from `role_permissions` where `role_id` = ?";
    $deleteQuery = $mysqli->prepare($sql);
    if ($deleteQuery === false) { // drat
        die('deleteQuery: '. $mysqli->error);
    }
    $deleteQuery->bind_param('s', $_POST['roleId']);

    $allOk = $deleteQuery->execute();
    if (!$allOk) { // drat
        die('deleteQuery: '. $deleteQuery->error);
    }

    $sql = "insert into role_permissions (role_id, permission_id) values (?, ?)";

    $insertQuery =  $mysqli->prepare($sql);
    if ($insertQuery === false) { // drat
        die('insertQuery: '. $mysqli->error);
    }

    foreach($allPermissions as $permissionName => $active) {

        if ($active) {
            // bind the variables to the ALREADY PREPARED query
            $insertQuery->bind_param('is', $_POST['roleId'], $permissionName);

            $allOk = $insertQuery->execute();
            if (!$allOk) { // drat
                die("insertQuery: $permissionName : {$insertQuery->error}");
            }
        }
    }
    echo "<br />role:  {$_POST['roleId']}: now has:<br />";
    foreach($allPermissions as $permissionName => $active) {
      echo $permissionName, ' : ', $active ? '<strong>Yippee!</strong>' : 'sadly, no', '<br />';
    }
}

?>
<form action="" method="post">
<div class="element"><!-- easy peasy way of sending out the current permissions -->
    <label for="permissions">Permissions:</label>
    <?php foreach(array_keys($allPermissions) as $permissionName): ?>
    <div>
    <label>
        <input type="checkbox" name="permissions[]"
                value="<?= $permissionName ?>"
                <?= $allPermissions[$permissionName]  ? 'checked="checked"' : ''; ?>
                >&nbsp;<?= $permissionName; ?></label>
    </div>
    <?php endforeach; ?>
</div>
<input type="hidden" name="roleId" value="1">
<input type="submit" name="goForIt" value="Go For It!">
</form>