beginTransaction(); // Initialize arrays $ver = $_POST['lectura'] ?? []; $editar = $_POST['editar'] ?? []; // Only proceed if $ver and $editar are not empty if (empty($ver) && empty($editar)) { throw new Exception("Both arrays are empty."); } $completo = []; // Process $editar array foreach ($editar as $edit) { $edit_separado = explode("_", $edit); $completo[] = $edit_separado; } // Call the function to delete all permissions $db->query("SELECT fd_permiso()"); // Process $ver array and merge with $completo foreach ($ver as $lectura) { $igual = false; $ver_separado = explode("_", $lectura); foreach ($completo as $comp) { if ($ver_separado[0] == $comp[0] && $ver_separado[1] == $comp[1]) { $igual = true; break; } } if (!$igual) { $completo[] = $ver_separado; } } // Insert the combined data into the 'permiso' table foreach ($completo as $actual) { $db->insert('permiso', [ 'pagina_id' => $actual[0], 'rol_id' => $actual[1], 'permiso_tipo' => $actual[2], ]); } // Commit the transaction $db->commit(); // Redirect to permisos.php header("Location: ../permisos.php"); exit(); } catch (Exception $e) { // Rollback the transaction on error $pdo->rollback(); // Handle the error (for example, log it or show an error message) echo "An error occurred: " . $e->getMessage(); exit(); } ?>