Building a Hotel Booking System with PHP and MySQL

In this tutorial, we will guide you through the process of building a Hotel Booking System using PHP and MySQL. This project will cover essential aspects of web development, including database design, user authentication, and dynamic content generation.

Prerequisites:

  • Basic understanding of PHP and MySQL.
  • A local development environment (e.g., XAMPP, WampServer).

Step 1: Database Design:

Start by designing the database. Create a MySQL database named hotel_booking and a table named rooms with fields like room_id, room_type, price, etc. Use the following SQL query:

CREATE DATABASE hotel_booking;
USE hotel_booking;

CREATE TABLE rooms (
    room_id INT PRIMARY KEY AUTO_INCREMENT,
    room_type VARCHAR(50) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    availability INT NOT NULL
);

Step 2: PHP Connection to MySQL:

Create a PHP file (db_connect.php) to establish a connection to the MySQL database:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hotel_booking";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Step 3: Displaying Available Rooms:

Create a PHP file (index.php) to display available rooms:

<?php
include("db_connect.php");

$sql = "SELECT * FROM rooms WHERE availability > 0";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "Room ID: " . $row["room_id"] . "<br>";
        echo "Room Type: " . $row["room_type"] . "<br>";
        echo "Price: $" . $row["price"] . "<br><br>";
    }
} else {
    echo "No available rooms.";
}

$conn->close();
?>

Step 4: User Authentication (Optional):

Implement user authentication for booking functionality. You can use PHP sessions or integrate a user authentication system.

Step 5: Booking a Room:

Create a PHP file (book_room.php) to handle room booking:

<?php
include("db_connect.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $room_id = $_POST["room_id"];
    
    // Perform booking logic (update availability, record booking details, etc.)

    echo "Room booked successfully!";
} else {
    echo "Invalid request.";
}

$conn->close();
?>

Congratulations! You’ve successfully built a basic Hotel Booking System using PHP and MySQL. This project can be expanded by adding features like user registration, booking history, and payment integration. Remember to sanitize user inputs, implement error handling, and enhance security features for a production-ready application. Feel free to customize and improve the system based on your project requirements.