35438-vm/admin_edit_tour.php
2025-11-03 08:24:38 +00:00

120 lines
4.4 KiB
PHP

<?php
require_once 'includes/admin_header.php';
require_once 'db/config.php';
$id = $_GET['id'] ?? null;
if (!$id) {
header('Location: admin_tours.php');
exit;
}
$name = $destination = $duration_days = $price = $description = '';
$errors = [];
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM tour_packages WHERE id = ?");
$stmt->execute([$id]);
$tour = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$tour) {
header('Location: admin_tours.php?error=notfound');
exit;
}
$name = $tour['name'];
$destination = $tour['destination'];
$duration_days = $tour['duration_days'];
$price = $tour['price'];
$description = $tour['description'];
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$destination = trim($_POST['destination'] ?? '');
$duration_days = trim($_POST['duration_days'] ?? '');
$price = trim($_POST['price'] ?? '');
$description = trim($_POST['description'] ?? '');
if (empty($name)) {
$errors[] = 'Package name is required.';
}
if (empty($destination)) {
$errors[] = 'Destination is required.';
}
if (!filter_var($duration_days, FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]])) {
$errors[] = 'Duration must be a positive number.';
}
if (!filter_var($price, FILTER_VALIDATE_INT, ["options" => ["min_range" => 0]])) {
$errors[] = 'Price must be a non-negative number.';
}
if (empty($errors)) {
try {
$sql = "UPDATE tour_packages SET name = ?, destination = ?, duration_days = ?, price = ?, description = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $destination, $duration_days, $price, $description, $id]);
header("Location: admin_tours.php?success=2");
exit;
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
require_once 'includes/admin_sidebar.php';
?>
<h1 class="h2">Edit Tour Package</h1>
<p>Update the details for the tour package below.</p>
<div class="card">
<div class="card-header">
<h5 class="mb-0">Tour Package Details</h5>
</div>
<div class="card-body">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="admin_edit_tour.php?id=<?php echo $id; ?>" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Package 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="destination" class="form-label">Destination</label>
<input type="text" class="form-control" id="destination" name="destination" value="<?php echo htmlspecialchars($destination); ?>" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="duration_days" class="form-label">Duration (days)</label>
<input type="number" class="form-control" id="duration_days" name="duration_days" value="<?php echo htmlspecialchars($duration_days); ?>" required min="1">
</div>
<div class="col-md-6 mb-3">
<label for="price" class="form-label">Price (Rp)</label>
<input type="number" class="form-control" id="price" name="price" value="<?php echo htmlspecialchars($price); ?>" required min="0">
</div>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($description); ?></textarea>
</div>
<hr>
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="admin_tours.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
<?php
require_once 'includes/admin_footer.php';
?>