33951-vm/edit-vendor.php
Flatlogic Bot 0a0873a371 v2
2025-09-09 15:57:31 +00:00

126 lines
5.2 KiB
PHP

<?php
require_once 'db/config.php';
$errorMessage = null;
$successMessage = null;
$vendor_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$vendor_id) {
header("Location: vendors.php");
exit;
}
// Handle form submission for editing the vendor
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_vendor'])) {
$name = trim($_POST['name'] ?? '');
$service_type = trim($_POST['service_type'] ?? '');
$contact_email = filter_input(INPUT_POST, 'contact_email', FILTER_VALIDATE_EMAIL);
$contact_phone = trim($_POST['contact_phone'] ?? '');
$rating = filter_input(INPUT_POST, 'rating', FILTER_VALIDATE_FLOAT);
if (empty($name) || empty($service_type) || !$contact_email) {
$errorMessage = "Error: Vendor name, service type, and a valid email are required.";
} else {
try {
$pdo = db();
$stmt = $pdo->prepare('UPDATE vendors SET name = ?, service_type = ?, contact_email = ?, contact_phone = ?, rating = ? WHERE id = ?');
$stmt->execute([$name, $service_type, $contact_email, $contact_phone, $rating, $vendor_id]);
header("Location: vendors.php?success=3");
exit;
} catch (PDOException $e) {
$errorMessage = "Error: Could not update the vendor. " . $e->getMessage();
}
}
}
// Fetch the vendor to edit
$vendor = null;
try {
$pdo = db();
$stmt = $pdo->prepare('SELECT * FROM vendors WHERE id = ?');
$stmt->execute([$vendor_id]);
$vendor = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$errorMessage = "Error: Could not fetch vendor details.";
}
if (!$vendor) {
header("Location: vendors.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Vendor - The Best Events</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">The Best Events</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="venues.php">Venues</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="vendors.php">Vendors</a>
</li>
</ul>
</div>
</div>
</nav>
<main class="container mt-4">
<h1 class="mb-4">Edit Vendor</h1>
<?php if ($errorMessage): ?>
<div class="alert alert-danger">
<?= htmlspecialchars($errorMessage) ?>
</div>
<?php endif; ?>
<form action="edit-vendor.php?id=<?= $vendor_id ?>" method="POST">
<input type="hidden" name="edit_vendor" value="1">
<div class="mb-3">
<label for="name" class="form-label">Vendor Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($vendor['name']) ?>" required>
</div>
<div class="mb-3">
<label for="service_type" class="form-label">Service Type</label>
<input type="text" class="form-control" id="service_type" name="service_type" value="<?= htmlspecialchars($vendor['service_type']) ?>" placeholder="e.g., Catering, Photography" required>
</div>
<div class="mb-3">
<label for="contact_email" class="form-label">Contact Email</label>
<input type="email" class="form-control" id="contact_email" name="contact_email" value="<?= htmlspecialchars($vendor['contact_email']) ?>" required>
</div>
<div class="mb-3">
<label for="contact_phone" class="form-label">Contact Phone</label>
<input type="tel" class="form-control" id="contact_phone" name="contact_phone" value="<?= htmlspecialchars($vendor['contact_phone']) ?>">
</div>
<div class="mb-3">
<label for="rating" class="form-label">Rating (1-5)</label>
<input type="number" step="0.1" min="1" max="5" class="form-control" id="rating" name="rating" value="<?= htmlspecialchars($vendor['rating']) ?>">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
<a href="vendors.php" class="btn btn-secondary">Cancel</a>
</form>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>