How to develop Projects(HBM) Using PHP and Mysql
Part 11
✅ Module 3: Room Booking
Management
🎯 Purpose:
To allow users to search
for available rooms, view room details, and book rooms.
✅ Functionalities to Implement:
For Users (Customers):
- 🔍 Search
for available rooms by location, date, number of guests, etc.
- 🏨 View
room details (price, facilities, images, etc.)
- 📅 Book
a room (select check-in and check-out dates)
- 📜 View
booking history
- ❌ Cancel
a booking (based on cancellation policy)
For Admin/Hotel Owner:
- 🧾 View
all bookings
- ✅ Approve
or confirm bookings (optional)
- 📊 View
booking statistics/reports
📁 Suggested
File/Folder Structure:
/bookings/
├──
search_rooms.php #
Search/filter available rooms
├──
view_room.php
# Detailed view of a single room
├──
book_room.php
# Booking form
├──
my_bookings.php
# View user’s own bookings
├──
cancel_booking.php # Cancel a
booking
├──
manage_bookings.php # Admin/hotel
staff view of all bookings
🧮 Suggested Database
Tables:
1. bookings
CREATE TABLE bookings (
id INT
AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
room_id INT NOT NULL,
check_in DATE NOT NULL,
check_out DATE NOT NULL,
status
ENUM('Pending', 'Confirmed', 'Cancelled') DEFAULT 'Pending',
total_price DECIMAL(10,2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (room_id) REFERENCES rooms(id)
);
2. rooms (Already
Registered)
Make sure it includes:
availability
ENUM('Available', 'Booked') DEFAULT 'Available'
📂 /bookings/search_rooms.php
<?php include('../config/db_connect.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Search Rooms</title>
</head>
<body>
<h2>Search
Available Rooms</h2>
<form method="GET">
<label>Location:</label>
<input type="text" name="location"
required>
<label>Check-in:</label>
<input type="date" name="check_in"
required>
<label>Check-out:</label>
<input type="date" name="check_out"
required>
<label>Guests:</label>
<input type="number" name="guests"
required>
<button type="submit">Search</button>
</form>
<?php
if (isset($_GET['location']))
{
$location = $_GET['location'];
$check_in = $_GET['check_in'];
$check_out = $_GET['check_out'];
$guests = $_GET['guests'];
$sql = "SELECT * FROM rooms WHERE
location LIKE '%$location%' AND availability = 'Available'";
$result = $conn->query($sql);
echo "<h3>Available
Rooms:</h3>";
echo "<ul>";
while
($row = $result->fetch_assoc()) {
echo "<li>
<a href='view_room.php?id={$row['id']}'>
Room {$row['room_number']} - {$row['price']}
USD
</a></li>";
}
echo "</ul>";
}
?>
</body>
</html>
📂 /bookings/view_room.php
<?php
include('../config/db_connect.php');
$id = $_GET['id'];
$result = $conn->query("SELECT
* FROM rooms WHERE id = $id");
$room = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Room Details</title>
</head>
<body>
<h2>Room
Details</h2>
<p><b>Room
Number:</b> <?= $room['room_number']; ?></p>
<p><b>Location:</b>
<?= $room['location']; ?></p>
<p><b>Price:</b>
<?= $room['price']; ?> USD per night</p>
<p><b>Facilities:</b>
<?= $room['facilities']; ?></p>
<p><b>Status:</b>
<?= $room['availability']; ?></p>
<?php if ($room['availability']
== 'Available'): ?>
<a href="book_room.php?room_id=<?=
$room['id']; ?>">Book This Room</a>
<?php else: ?>
<p>Not Available</p>
<?php endif; ?>
</body>
</html>
📂 /bookings/book_room.php
<?php
include('../config/db_connect.php');
session_start(); //
Assuming login system sets $_SESSION['user_id']
$user_id = $_SESSION['user_id']
?? 1; // Hardcoded for test
$room_id = $_GET['room_id'];
if ($_SERVER['REQUEST_METHOD']
=== 'POST') {
$check_in = $_POST['check_in'];
$check_out = $_POST['check_out'];
$status = 'Pending';
$days = (strtotime($check_out) - strtotime($check_in))
/ (60 * 60 * 24);
$room = $conn->query("SELECT price
FROM rooms WHERE id=$room_id")->fetch_assoc();
$total_price = $room['price'] * $days;
$sql = "INSERT INTO bookings (user_id,
room_id, check_in, check_out, status, total_price)
VALUES ($user_id, $room_id, '$check_in',
'$check_out', '$status', $total_price)";
if ($conn->query($sql)) {
echo "Room booked successfully!
<a href='my_bookings.php'>My Bookings</a>";
} else {
echo "Error: " . $conn->error;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book Room</title>
</head>
<body>
<h2>Book
Room</h2>
<form method="POST">
<label>Check-in Date:</label>
<input type="date" name="check_in"
required><br>
<label>Check-out Date:</label>
<input type="date" name="check_out"
required><br>
<button type="submit">Confirm
Booking</button>
</form>
</body>
</html>
📂 /bookings/my_bookings.php
<?php
include('../config/db_connect.php');
session_start();
$user_id = $_SESSION['user_id']
?? 1; // test user
$result = $conn->query("SELECT
b.*, r.room_number FROM bookings b JOIN rooms r ON b.room_id = r.id WHERE
user_id = $user_id");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Bookings</title>
</head>
<body>
<h2>My
Bookings</h2>
<table border="1">
<tr><th>Room</th><th>Check-in</th><th>Check-out</th><th>Status</th><th>Action</th></tr>
<?php while($row = $result->fetch_assoc()):
?>
<tr>
<td><?= $row['room_number']; ?></td>
<td><?= $row['check_in']; ?></td>
<td><?= $row['check_out']; ?></td>
<td><?= $row['status']; ?></td>
<td>
<?php if($row['status'] == 'Pending'):
?>
<a href="cancel_booking.php?id=<?=
$row['id']; ?>" onclick="return confirm('Cancel this
booking?')">Cancel</a>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
📂 /bookings/cancel_booking.php
<?php
include('../config/db_connect.php');
$id = $_GET['id'];
$sql = "UPDATE
bookings SET status = 'Cancelled' WHERE id = $id";
if ($conn->query($sql))
{
header('Location: my_bookings.php');
} else {
echo "Error cancelling booking.";
}
?>
📂 /bookings/manage_bookings.php
<?php
include('../config/db_connect.php');
$result = $conn->query("SELECT
b.*, u.name, r.room_number FROM bookings b JOIN users u ON b.user_id = u.id
JOIN rooms r ON b.room_id = r.id");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Manage Bookings</title>
</head>
<body>
<h2>All Bookings
(Admin View)</h2>
<table border="1">
<tr><th>User</th><th>Room</th><th>Check-in</th><th>Check-out</th><th>Status</th><th>Total
Price</th></tr>
<?php while($row = $result->fetch_assoc()):
?>
<tr>
<td><?= $row['name']; ?></td>
<td><?= $row['room_number']; ?></td>
<td><?= $row['check_in']; ?></td>
<td><?= $row['check_out']; ?></td>
<td><?= $row['status']; ?></td>
<td>$<?= $row['total_price']; ?></td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>