HEX
Server: nginx/1.27.0
System: Linux ns31201788 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: www-data (33)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: /var/www/afadcavo/cliente_delete.php
<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit();
}

if (!isset($_GET['id'])) {
    header("Location: clienti.php");
    exit();
}

$id = intval($_GET['id']);

$host = 'localhost';
$dbname = 'securedoor';
$username_db = 'securedoor';
$password_db = 'securedoor';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username_db, $password_db);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Controllo se esistono contratti o pagamenti associati
    $checkContratti = $pdo->prepare("SELECT COUNT(*) FROM contratti WHERE id_cliente = ?");
    $checkContratti->execute([$id]);
    $hasContratti = $checkContratti->fetchColumn() > 0;

    $checkPagamenti = $pdo->prepare("SELECT COUNT(*) FROM pagamenti WHERE id_cliente = ?");
    $checkPagamenti->execute([$id]);
    $hasPagamenti = $checkPagamenti->fetchColumn() > 0;

    if ($hasContratti || $hasPagamenti) {
        // Non eliminare: reindirizza con errore
        header("Location: clienti.php?errore=cliente_con_relazioni");
        exit();
    }

    // Procedi con l'eliminazione
    $delete = $pdo->prepare("DELETE FROM anagrafica WHERE id = ?");
    $delete->execute([$id]);

    header("Location: clienti.php?success=cliente_eliminato");
    exit();

} catch (PDOException $e) {
    die("Errore DB: " . $e->getMessage());
}
?>