-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancel_booking.php
46 lines (38 loc) · 1.52 KB
/
cancel_booking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
include("../header.php");
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("Location: login.php");
exit;
}
if (isset($_POST['cancel_booking'])) {
$reservationId = $_POST['reservation_id'];
$userId = $_SESSION['user_id'];
// Check if the reservation belongs to the logged-in user
$checkQuery = "SELECT * FROM reservations WHERE reservation_id = $reservationId AND user_id = $userId";
$checkResult = $con->query($checkQuery);
if ($checkResult && $checkResult->num_rows === 1) {
// Fetch the associated home_id
$reservationRow = $checkResult->fetch_assoc();
$homeId = $reservationRow['home_id'];
// Delete the reservation
$deleteQuery = "DELETE FROM reservations WHERE reservation_id = $reservationId";
$deleteResult = $con->query($deleteQuery);
if ($deleteResult) {
// Update the availability_status to "available" for the associated home
$updateAvailabilityQuery = "UPDATE home SET availability_status = 'available' WHERE home_id = $homeId";
$updateAvailabilityResult = $con->query($updateAvailabilityQuery);
if ($updateAvailabilityResult) {
header("Location: reservations.php");
} else {
echo "Error updating availability status.";
}
} else {
echo "Error canceling booking.";
}
} else {
echo "Invalid reservation.";
}
} else {
header("Location: reservations.php");
}
?>