35251-vm/create_customer.php
2025-10-26 16:47:49 +00:00

110 lines
4.3 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'templates/header.php';
$name = $email = $phone = $address = $plan_id = "";
$errors = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$phone = trim($_POST["phone"]);
$address = trim($_POST["address"]);
$plan_id = trim($_POST["plan_id"]);
if (empty($name)) {
$errors[] = "Name is required.";
}
if (empty($email)) {
$errors[] = "Email is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format.";
}
if (empty($errors)) {
try {
$pdo = db();
$sql = "INSERT INTO customers (name, email, phone, address, plan_id, created_at) VALUES (?, ?, ?, ?, ?, NOW())";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $email, $phone, $address, $plan_id]);
header("Location: customers.php");
exit;
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
// Fetch plans for the dropdown
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name FROM plans");
$plans = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$plans = [];
$errors[] = "Could not fetch plans.";
}
?>
<div class="container-fluid px-4">
<h1 class="mt-4">Create New Customer</h1>
<ol class="breadcrumb mb-4">
<li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
<li class="breadcrumb-item"><a href="customers.php">Customers</a></li>
<li class="breadcrumb-item active">Create</li>
</ol>
<div class="card mb-4">
<div class="card-header">
<i class="fas fa-user-plus me-1"></i>
New Customer Details
</div>
<div class="card-body">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo htmlspecialchars($error); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form action="create_customer.php" method="post">
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($phone); ?>">
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" id="address" name="address" rows="3"><?php echo htmlspecialchars($address); ?></textarea>
</div>
<div class="mb-3">
<label for="plan_id" class="form-label">Subscription Plan</label>
<select class="form-select" id="plan_id" name="plan_id">
<option value="">Select a plan</option>
<?php foreach ($plans as $plan): ?>
<option value="<?php echo htmlspecialchars($plan['id']); ?>" <?php echo ($plan_id == $plan['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($plan['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Create Customer</button>
<a href="customers.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
</div>
<?php require_once 'templates/footer.php'; ?>