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();
}
}
請不要接受你不滿意的答案!包括這一個!你不需要接受答案。我做代碼'因爲我喜歡這樣做。我可能需要自己的代碼,沒有它。我會用它來「真實」。顯然,我很喜歡它。 :-)其他人做'填字遊戲',我這樣做。 :-) –