14 - Hotel Booking Website using PHP and MySQL | Rooms Backend - Part 1
Table of Contents
Introduction
In this tutorial, we will guide you through creating the backend functionalities for the rooms page of a hotel booking website using PHP and MySQL. This includes adding and editing room details, changing room statuses, and completing the features and facilities section. By the end of this tutorial, you'll have a solid foundation for managing room data on your hotel booking website.
Step 1: Setting Up the Rooms Page Design
- Begin by creating the HTML structure for the rooms page.
- Include necessary elements such as:
- A table to display room details (room ID, name, status, etc.).
- Buttons for adding, editing, and deleting rooms.
- Ensure the design is user-friendly and responsive.
Step 2: Connecting to the Database
- Establish a connection to your MySQL database using PHP.
- Use the following code snippet:
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
- Replace
your_username
,your_password
, andyour_database
with your actual database credentials.
Step 3: Adding Room Functionality
- Create a form for adding new rooms, including fields for room name, type, price, and status.
- Handle form submission in PHP to insert room data into the database:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$room_name = $_POST['room_name'];
$room_type = $_POST['room_type'];
$room_price = $_POST['room_price'];
$room_status = $_POST['room_status'];
$sql = "INSERT INTO rooms (name, type, price, status) VALUES ('$room_name', '$room_type', '$room_price', '$room_status')";
if ($conn->query($sql) === TRUE) {
echo "New room added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
Step 4: Editing Room Functionality
- Implement an edit feature that allows users to modify existing room details.
- Create an edit form pre-filled with the current room data.
- Use the following code to update the database:
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['edit'])) {
$room_id = $_POST['room_id'];
$room_name = $_POST['room_name'];
$room_type = $_POST['room_type'];
$room_price = $_POST['room_price'];
$room_status = $_POST['room_status'];
$sql = "UPDATE rooms SET name='$room_name', type='$room_type', price='$room_price', status='$room_status' WHERE id='$room_id'";
if ($conn->query($sql) === TRUE) {
echo "Room updated successfully";
} else {
echo "Error updating room: " . $conn->error;
}
}
Step 5: Changing Room Status
- Allow users to change the status of rooms to active or inactive.
- Add a status toggle button in the rooms table.
- Implement the following code to update the room status in the database:
if (isset($_POST['change_status'])) {
$room_id = $_POST['room_id'];
$new_status = $_POST['new_status']; // 'active' or 'inactive'
$sql = "UPDATE rooms SET status='$new_status' WHERE id='$room_id'";
if ($conn->query($sql) === TRUE) {
echo "Room status updated successfully";
} else {
echo "Error updating status: " . $conn->error;
}
}
Step 6: Completing Features and Facilities Section
- Develop a section to manage features and facilities associated with rooms.
- Create a form for adding features and link them to specific rooms in the database.
- Use similar insert/update logic as in previous steps to manage this data.
Conclusion
In this tutorial, we have covered the essential backend functionalities for managing rooms in a hotel booking website using PHP and MySQL. You learned how to set up the rooms page, add and edit room details, change room statuses, and manage features and facilities.
Next steps could include enhancing the user interface, implementing security measures, or adding user authentication. For further learning, explore the provided YouTube links for additional functionalities like image uploads and user registration systems.