exec("CREATE TABLE IF NOT EXISTS contacts ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, phone VARCHAR(50), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); // Handle POST requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Add a new contact if (isset($_POST['add_contact'])) { $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); if (!empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) { $stmt = $pdo->prepare("INSERT INTO contacts (name, email, phone) VALUES (?, ?, ?)"); $stmt->execute([$name, $email, $phone]); $_SESSION['notification'] = ['type' => 'success', 'message' => 'Contact added successfully!']; } else { $_SESSION['notification'] = ['type' => 'danger', 'message' => 'Invalid data. Please check your input.']; } } // Update an existing contact elseif (isset($_POST['update_contact'])) { $id = $_POST['contact_id']; $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); if (!empty($id) && !empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) { $stmt = $pdo->prepare("UPDATE contacts SET name = ?, email = ?, phone = ? WHERE id = ?"); $stmt->execute([$name, $email, $phone, $id]); $_SESSION['notification'] = ['type' => 'success', 'message' => 'Contact updated successfully!']; } else { $_SESSION['notification'] = ['type' => 'danger', 'message' => 'Invalid data. Please check your input.']; } } // Delete a contact elseif (isset($_POST['delete_contact'])) { $id = $_POST['contact_id']; if (!empty($id)) { $stmt = $pdo->prepare("DELETE FROM contacts WHERE id = ?"); $stmt->execute([$id]); $_SESSION['notification'] = ['type' => 'info', 'message' => 'Contact deleted.']; } } header("Location: index.php"); exit; } // Check for notification from session if (isset($_SESSION['notification'])) { $notification = $_SESSION['notification']; unset($_SESSION['notification']); } // Fetch all contacts $stmt = $pdo->query("SELECT id, name, email, phone, created_at FROM contacts ORDER BY created_at DESC"); $contacts = $stmt->fetchAll(); } catch (PDOException $e) { // For a real app, you'd log this error and show a user-friendly message. $notification = ['type' => 'danger', 'message' => 'Database error: ' . $e->getMessage()]; $contacts = []; } ?> CRM - Contact Management

Add New Contact

Please enter a name.
Please enter a valid email address.

Contact List

Name Email Phone Date Added Actions
No contacts yet. Add one to get started!