我正在制作一個專案,其中他們是具有不同角色和權限的多個用戶。每個用戶根據他的角色都有權限(訪問、創建、更新、洗掉)。我已經撰寫了一段代碼,但是每當我嘗試更新角色權限時,它都不會更新正確的表列。
#PERMISSIONS 表結構

#權限表
<form id="updatePermissionForm">
<input type="hidden" value="<?= $role_id ?>" name="role_id">
<input type="hidden" value="update_role" name="action">
<div class="table-responsive">
<table class="table table-striped custom-table">
<thead>
<tr>
<th class="fw-bolder">Module Permission</th>
<th class="text-center fw-bolder">Accès</th>
<th class="text-center fw-bolder">Ecrire</th>
<th class="text-center fw-bolder">Modifier</th>
<th class="text-center fw-bolder">Supprimer</th>
</tr>
</thead>
<tbody>
<?php foreach($permissions as $prm) : ?>
<tr>
<td>
<i class="ti-folder"></i> <?= moduleName($prm->module_id);?>
<input type="hidden" value="<?= $prm->module_id ?>" name="module_id[]" >
</td>
<td class="text-center">
<input class="access_module" type="checkbox" <?= ($prm->can_access) ? 'checked="checked"' : ''; ?> name="can_access[]" value="<?= $prm->can_access ?>">
</td>
<td class="text-center">
<input type="checkbox" <?= ($prm->can_create) ? 'checked="checked"' : ''; ?> name="can_create[]" value="<?= $prm->can_create ?>">
</td>
<td class="text-center">
<input type="checkbox" <?= ($prm->can_update) ? 'checked="checked"' : ''; ?> name="can_update[]" value="<?= $prm->can_update ?>">
</td>
<td class="text-center">
<input type="checkbox" <?= ($prm->can_delete) ? 'checked="checked"' : ''; ?> name="can_delete[]" value="<?= $prm->can_delete ?>">
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="m-t-20 text-center">
<button class="btn btn-primary btn-lg" id="updatePermissionBtn" type="submit" >Sauvegarder</button>
</div>
</form>

#AJAX
$(document.body).on('submit', "#updatePermissionForm", function(e){
e.preventDefault()
$.ajax({
type: "POST",
url: `ajax/roles/roles_actions.php`,
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData: false,
dataType: "json",
beforeSend: function() {
$("#updatePermissionBtn").prop("disabled", true);
$("#updatePermissionBtn").html('<i ></i> Sauvegarde en cours ...');
},
success: function(response) {
$("#updatePermissionBtn").prop("disabled", false);
$("#updatePermissionBtn").html("Sauvegader");
if (response.status == 1) {
Swal.fire("Succès!", response.message, "success");
} else {
Swal.fire("Attention!", response.message, "error");
}
},
});
});
#roles_actions.php
if(isset($_POST["action"])){
if($_POST['action'] == 'update_role'){
foreach ($_POST['module_id'] as $key => $value) {
$query = "UPDATE `permissions` SET
can_access=:can_access,
can_create=:can_create,
can_update=:can_update,
can_delete=:can_delete
WHERE role_id=:role_id
AND module_id=:module_id
";
$can_access = empty($_POST['can_access'][$value]) ? 0 : 1;
$can_create = empty($_POST['can_create'][$value]) ? 0 : 1;
$can_update = empty($_POST['can_update'][$value]) ? 0 : 1;
$can_delete = empty($_POST['can_delete'][$value]) ? 0 : 1;
$stmt = $PDO->prepare($query);
$stmt->bindParam(':can_access', $can_access, PDO::PARAM_INT);
$stmt->bindParam(':can_create', $can_create, PDO::PARAM_INT);
$stmt->bindParam(':can_update', $can_update, PDO::PARAM_INT);
$stmt->bindParam(':can_delete', $can_delete, PDO::PARAM_INT);
$stmt->bindParam(':role_id' , $_POST['role_id'] , PDO::PARAM_INT);
$stmt->bindParam(':module_id' ,$_POST['module_id'][$key] , PDO::PARAM_INT);
$stmt->execute();
if($stmt){
$response['status'] = 1;
$response['message'] = 'Permission du r?le ont été mise à jour avec succès!';
}
}
}
}
echo json_encode($response);
我在這里錯過了什么嗎?
uj5u.com熱心網友回復:
您應該在解決方案中更改兩件事:
- 在
<form>改變你如何構建你的name屬性時,從這里:
<input ... name="can_update[]" value="<?= $prm->can_update ?>">
像這樣:
<input ... name="permission[<?= $prm->module_id ?>][can_update]" value="1">
這將產生類似這樣的東西(例如module_id = 2):
<input type="checkbox" checked name="permission[2][can_update]" value="1">
并將通過 php 在 php 中訪問$_POST['permission'][2]['can_update']=1
- 然后在
roles_actions.php:
更改搜索權限的方式$_POST:
<?php
...
foreach ($_POST['module_id'] as $module_id) {
...
$can_update = empty($_POST['permission'][$module_id]['can_update']) ? 0 : 1;
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454720.html
