getConnection(); function e($value): string { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); } $message = $_GET['message'] ?? 'N'; $insertFeedback = null; /* ------------------------------------------------------------------------- * Inserimento nuova classe (POST) con prepared statement * ---------------------------------------------------------------------- */ if (isset($_POST['classinsert']) && $_POST['classinsert'] === 'Y') { $servicename = trim($_POST['servicename'] ?? ''); $wpcatalog = $_POST['wpcatalognumber'] ?? ''; $day = trim($_POST['classdayday'] ?? ''); $time = trim($_POST['classtime'] ?? ''); $category = $_POST['servicecategory'] ?? ''; $maxcapacity = $_POST['maxcapacity'] ?? ''; $colorclass = trim($_POST['colorclass'] ?? ''); $classduration = $_POST['classduration'] ?? ''; if ($servicename === '') { $insertFeedback = ['type' => 'error', 'text' => 'La descrizione della classe è obbligatoria.']; } else { $sql = "INSERT INTO service (servicename, wpcatalognumber, day, time, category, maxcapacity, colorclass, classduration) VALUES (:servicename, :wpcatalog, :day, :time, :category, :maxcapacity, :colorclass, :classduration)"; $stmt = $pdo->prepare($sql); $ok = $stmt->execute([ ':servicename' => $servicename, ':wpcatalog' => $wpcatalog !== '' ? (int) $wpcatalog : null, ':day' => $day, ':time' => $time, ':category' => $category !== '' ? (int) $category : null, ':maxcapacity' => $maxcapacity !== '' ? (int) $maxcapacity : null, ':colorclass' => $colorclass, ':classduration' => $classduration !== '' ? (float) $classduration : null, ]); if ($ok) { // Redirect per evitare re-invio del form al refresh (pattern PRG) header('Location: ' . $_SERVER['PHP_SELF'] . '?message=inserted'); exit; } $insertFeedback = ['type' => 'error', 'text' => 'Errore durante l\'inserimento della classe. Riprova.']; } } /* ------------------------------------------------------------------------- * Categorie per il dropdown * ---------------------------------------------------------------------- */ $categories = $pdo->query("SELECT idservicecategory, namecategory FROM servicecategory ORDER BY namecategory")->fetchAll(); /* ------------------------------------------------------------------------- * Elenco classi esistenti * ---------------------------------------------------------------------- */ $services = $pdo->query( "SELECT service.*, servicecategory.namecategory FROM service LEFT JOIN servicecategory ON service.category = servicecategory.idservicecategory ORDER BY service.servicename" )->fetchAll(); ?>