<?php
include('include/headscript.php');
$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
    die("Errore di connessione: " . $conn->connect_error);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $idrole = intval($_POST['idrole']);
    $role_name = $conn->real_escape_string($_POST['role_name']);
    $description = $conn->real_escape_string($_POST['description']);
    $permissions = $conn->real_escape_string($_POST['permissions']);

    $query = "UPDATE sharing_roles SET role_name = ?, description = ?, permissions = ? WHERE idrole = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param('sssi', $role_name, $description, $permissions, $idrole);

    if ($stmt->execute()) {
        header("Location: sharing-roles.php?success=true");
    } else {
        header("Location: sharing-roles.php?error=true");
    }

    $stmt->close();
}
$conn->close();
