37122-vm/customers.php
Flatlogic Bot 045393627b Billing
2025-12-22 17:18:47 +00:00

145 lines
5.8 KiB
PHP

<?php
require_once 'db/config.php';
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
try {
$pdo = db();
$sql = "INSERT INTO customers (name, email, phone, address) VALUES (?, ?, ?, ?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$name, $email, $phone, $address]);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
}
// Create table if not exists
try {
$pdo = db();
$sql = "CREATE TABLE IF NOT EXISTS customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL,
address TEXT NOT NULL,
status VARCHAR(255) DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
$pdo->exec($sql);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
// Fetch customers
try {
$pdo = db();
$stmt = $pdo->query("SELECT * FROM customers ORDER BY created_at DESC");
$customers = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customer Management</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
body {
font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: #F8F9FA;
}
</style>
</head>
<body>
<div class="container mt-5">
<h1 class="mb-4">Customer Management</h1>
<div class="card mb-4">
<div class="card-header">
Add New Customer
</div>
<div class="card-body">
<form action="customers.php" method="post">
<div class="row">
<div class="col-md-6 mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-6 mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" required>
</div>
<div class="col-md-6 mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" id="address" name="address" rows="3" required></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">Add Customer</button>
</form>
</div>
</div>
<div class="card">
<div class="card-header">
Customer List
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Status</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
<?php if (!empty($customers)): ?>
<?php foreach ($customers as $customer): ?>
<tr>
<td><?php echo htmlspecialchars($customer['id']); ?></td>
<td><?php echo htmlspecialchars($customer['name']); ?></td>
<td><?php echo htmlspecialchars($customer['email']); ?></td>
<td><?php echo htmlspecialchars($customer['phone']); ?></td>
<td><?php echo htmlspecialchars($customer['address']); ?></td>
<td><span class="badge bg-success"><?php echo htmlspecialchars($customer['status']); ?></span></td>
<td><?php echo htmlspecialchars($customer['created_at']); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="7" class="text-center">No customers found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>