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());
}
?>