0
我的目标:编辑中间表复选框
要改变我的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=""> canPushAPK</label>
<input type="hidden" name="permissions[2]" value="false">
<label>
<input type="checkbox" name="permissions[2]" value="true"> canBeCool</label>
<input type="hidden" name="permissions[3]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[3]" value="true" checked=""> canEditSettings</label>
<input type="hidden" name="permissions[4]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[4]" value="true" checked=""> canManageRoles</label>
<input type="hidden" name="permissions[5]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[5]" value="true" checked=""> canAddUser</label>
<input type="hidden" name="permissions[6]" value="false">
<label>
<input type="checkbox" name="permissions[6]" value="true"> canFoo</label>
<input type="hidden" name="permissions[7]" value="false">
<label>
<input type="checkbox" name="permissions[7]" value="true"> canTalk</label>
<input type="hidden" name="permissions[8]" value="false">
<label>
<input type="checkbox" name="permissions[8]" value="true"> canTest</label>
<input type="hidden" name="permissions[9]" value="false">
<label>
<input type="checkbox" name="permissions[9]" value="true"> canPoo</label>
</div>
<input type="hidden" name="roleId" value="1">
然后根据什么值检查“roleId”= 1,它插入数据库。对于所有未选中的项目,它将删除此行。
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();
}
}
请不要接受你不满意的答案!包括这一个!你不需要接受答案。我做代码'因为我喜欢这样做。我可能需要自己的代码,没有它。我会用它来“真实”。显然,我很喜欢它。 :-)其他人做'填字游戏',我这样做。 :-) –