35050-vm/delete_contact.php
Flatlogic Bot b4397b355f Sfepro v1
2025-10-19 03:26:15 +00:00

41 lines
1.2 KiB
PHP

<?php
require_once 'db/config.php';
session_start();
if (!isset($_GET['id']) || empty($_GET['id'])) {
header("Location: contacts.php");
exit;
}
$contact_id = $_GET['id'];
try {
$pdo = db();
// Check for associated deals
$stmt = $pdo->prepare("SELECT COUNT(*) FROM deals WHERE contact_id = :contact_id");
$stmt->bindParam(':contact_id', $contact_id, PDO::PARAM_INT);
$stmt->execute();
$deal_count = $stmt->fetchColumn();
if ($deal_count > 0) {
$_SESSION['error_message'] = "Cannot delete contact. It is associated with {$deal_count} deal(s). Please reassign or delete the deals first.";
} else {
// No associated deals, proceed with deletion
$stmt = $pdo->prepare("DELETE FROM contacts WHERE id = :id");
$stmt->bindParam(':id', $contact_id, PDO::PARAM_INT);
if ($stmt->execute() && $stmt->rowCount() > 0) {
$_SESSION['success_message'] = "Contact deleted successfully.";
} else {
$_SESSION['error_message'] = "Failed to delete contact or contact not found.";
}
}
} catch (PDOException $e) {
$_SESSION['error_message'] = "Database error: " . $e->getMessage();
}
header("Location: contacts.php");
exit;
?>