Flight Booking Management System Source Code
Creating a Flight Booking Management System is a comprehensive project involving several components, including user interfaces, backend logic, and database management. Welcome to our comprehensive guide on building and managing an Online Flight Booking Management System in PHP and MySQL. In this tutorial, we’ll walk you through the process of setting up and running a robust web application that allows users to book flights seamlessly.
Whether you’re a seasoned developer or a novice, this guide will provide you with all the necessary steps to get started. Below is a basic example of how to create such a system using PHP and MySQL. This example will cover essential features like searching for flights, booking a flight, and viewing bookings.
Set Up the Project Directory
Create a project directory with the following structure:
flight-booking-system/
├── config.php
├── db.sql
├── index.php
├── search.php
├── book.php
├── view-booking.php
└── styles.css
Database Configuration
Create a db.sql
file to set up the database schema:
CREATE DATABASE flight_booking;
USE flight_booking;
CREATE TABLE flights (
id INT AUTO_INCREMENT PRIMARY KEY,
flight_number VARCHAR(10) NOT NULL,
origin VARCHAR(100) NOT NULL,
destination VARCHAR(100) NOT NULL,
departure_time DATETIME NOT NULL,
arrival_time DATETIME NOT NULL,
price DECIMAL(10, 2) NOT NULL
);
CREATE TABLE bookings (
id INT AUTO_INCREMENT PRIMARY KEY,
flight_id INT NOT NULL,
passenger_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
booking_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (flight_id) REFERENCES flights(id)
);
Database Connection
Create a config.php
file to handle the database connection:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "flight_booking";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Search for Flights
Create an index.php
file to search for flights:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flight Booking System</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Search Flights</h1>
<form action="search.php" method="GET">
<label for="origin">Origin:</label>
<input type="text" id="origin" name="origin" required>
<label for="destination">Destination:</label>
<input type="text" id="destination" name="destination" required>
<button type="submit">Search</button>
</form>
</body>
</html>
Handle Flight Search
Create a search.php
file to handle the flight search logic:
<?php
include 'config.php';
$origin = $_GET['origin'];
$destination = $_GET['destination'];
$sql = "SELECT * FROM flights WHERE origin='$origin' AND destination='$destination'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<h1>Available Flights</h1>";
echo "<table>";
echo "<tr><th>Flight Number</th><th>Origin</th><th>Destination</th><th>Departure</th><th>Arrival</th><th>Price</th><th>Book</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['flight_number']}</td>
<td>{$row['origin']}</td>
<td>{$row['destination']}</td>
<td>{$row['departure_time']}</td>
<td>{$row['arrival_time']}</td>
<td>{$row['price']}</td>
<td><a href='book.php?flight_id={$row['id']}'>Book</a></td>
</tr>";
}
echo "</table>";
} else {
echo "No flights found.";
}
?>
Book a Flight
Create a book.php
file to handle flight bookings:
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$flight_id = $_POST['flight_id'];
$passenger_name = $_POST['passenger_name'];
$email = $_POST['email'];
$sql = "INSERT INTO bookings (flight_id, passenger_name, email) VALUES ('$flight_id', '$passenger_name', '$email')";
if ($conn->query($sql) === TRUE) {
echo "Booking successful!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
$flight_id = $_GET['flight_id'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Flight</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Book Flight</h1>
<form action="book.php" method="POST">
<input type="hidden" name="flight_id" value="<?php echo $flight_id; ?>">
<label for="passenger_name">Name:</label>
<input type="text" id="passenger_name" name="passenger_name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<button type="submit">Book</button>
</form>
</body>
</html>
View Bookings
Create a view-booking.php
file to view bookings:
<?php
include 'config.php';
$sql = "SELECT b.id, f.flight_number, f.origin, f.destination, f.departure_time, b.passenger_name, b.email, b.booking_time
FROM bookings b
JOIN flights f ON b.flight_id = f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<h1>View Bookings</h1>";
echo "<table>";
echo "<tr><th>Booking ID</th><th>Flight Number</th><th>Origin</th><th>Destination</th><th>Departure</th><th>Passenger Name</th><th>Email</th><th>Booking Time</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['flight_number']}</td>
<td>{$row['origin']}</td>
<td>{$row['destination']}</td>
<td>{$row['departure_time']}</td>
<td>{$row['passenger_name']}</td>
<td>{$row['email']}</td>
<td>{$row['booking_time']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No bookings found.";
}
?>
Styling
Create a styles.css
file to add basic styles:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
}
form {
display: flex;
flex-direction: column;
max-width: 300px;
margin: 0 auto;
}
label {
margin-top: 10px;
}
input, button {
padding: 10px;
margin-top: 5px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
}
th {
background-color: #f2f2f2;
text-align: center;
}
How To Run
To run this project successfully, follow the step-by-step instructions below
Step 1: Setup Environment
Ensure you have a virtual server installed, such as XAMPP, on your PC to host the project.
Step 2: Project Setup
- Extract the project files to a designated folder on your local machine.
- Copy the main project folder to the ‘htdocs’ directory within your XAMPP installation folder.
Step 3: Database Configuration
- Open your web browser and navigate to “http://localhost/phpmyadmin/“.
- Click on the “Databases” tab and create a new database named “flight_booking_db”.
- Import the provided SQL file (“flight_booking_db.sql”) located inside the “db” folder into the newly created database.
- Click on the “Go” button to initiate the import process.
Step 4: Accessing the Application
- Open your web browser and go to “http://localhost/Online_Flight_Booking_Management_System“.
- Use the following credentials to log in:
- Username: admin
- Password: admin123