36806-vm/update_lead.php
Flatlogic Bot 5366b4681b CRM-V1
2025-12-11 07:55:09 +00:00

61 lines
1.6 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: index.php');
exit();
}
// Basic validation
$lead_id = $_POST['lead_id'] ?? null;
$name = trim($_POST['name'] ?? '');
$companyName = trim($_POST['companyName'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$potentialAmount = $_POST['potentialAmount'] ?? null;
$status = $_POST['status'] ?? 'New';
if (empty($lead_id) || empty($name) || empty($email)) {
$_SESSION['error_message'] = 'Required fields are missing.';
header('Location: index.php');
exit();
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$_SESSION['error_message'] = 'Invalid email format.';
header('Location: edit_lead.php?id=' . $lead_id);
exit();
}
if ($potentialAmount !== null && !is_numeric($potentialAmount)) {
$_SESSION['error_message'] = 'Potential amount must be a number.';
header('Location: edit_lead.php?id=' . $lead_id);
exit();
}
try {
$pdo = db();
$stmt = $pdo->prepare(
'UPDATE leads SET Name = ?, CompanyName = ?, Email = ?, Phone = ?, PotentialAmount = ?, Status = ? WHERE LeadID = ?'
);
$stmt->execute([
$name,
$companyName,
$email,
$phone,
$potentialAmount,
$status,
$lead_id
]);
$_SESSION['success_message'] = 'Lead updated successfully!';
} catch (PDOException $e) {
// In a real app, log this error.
$_SESSION['error_message'] = 'Failed to update lead. Please try again.';
}
header('Location: index.php');
exit();