35251-vm/edit_customer.php
2025-10-26 16:53:16 +00:00

90 lines
3.0 KiB
PHP

<?php
require_once 'db/config.php';
$title = 'Edit Customer - Billing';
$page = 'customers';
require_once 'templates/header.php';
$customer_id = $_GET['id'] ?? null;
$error_message = '';
$success_message = '';
$customer = null;
if (!$customer_id) {
header('Location: customers.php');
exit;
}
try {
$pdo = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$status = trim($_POST['status']);
if (empty($name) || empty($email) || empty($status)) {
$error_message = 'Please fill in all required fields.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message = 'Invalid email format.';
} else {
$stmt = $pdo->prepare('UPDATE customers SET name = ?, email = ?, status = ? WHERE id = ?');
$stmt->execute([$name, $email, $status, $customer_id]);
$success_message = 'Customer updated successfully!';
}
}
$stmt = $pdo->prepare('SELECT id, name, email, status FROM customers WHERE id = ?');
$stmt->execute([$customer_id]);
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$customer) {
header('Location: customers.php');
exit;
}
} catch (PDOException $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
?>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Edit Customer</h1>
</div>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($success_message); ?></div>
<?php endif; ?>
<?php if ($customer): ?>
<form action="edit_customer.php?id=<?php echo htmlspecialchars($customer_id); ?>" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($customer['name']); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($customer['email']); ?>" required>
</div>
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status" required>
<option value="active" <?php echo ($customer['status'] === 'active') ? 'selected' : ''; ?>>Active</option>
<option value="inactive" <?php echo ($customer['status'] === 'inactive') ? 'selected' : ''; ?>>Inactive</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="customers.php" class="btn btn-secondary">Cancel</a>
</form>
<?php else: ?>
<p>Customer not found.</p>
<?php endif; ?>
<?php
require_once 'templates/footer.php';
?>