How to develop Projects (EMS)
Using PHP and Mysql
Part 12
Modify the Code :
- Add
the event price when booking.
- Generate
an auto token number after booking.
- Generate
a report (after booking) with all booking
details.
⚡ Important:
- Make
sure your bookings_table has these new columns:
ALTER TABLE
bookings_table
ADD COLUMN price DECIMAL(10,2),
ADD COLUMN token_number VARCHAR(50);
- Ensure
your events_table has a price field too:
ALTER TABLE events_table
ADD COLUMN price DECIMAL(10,2);
✅ I'll modify your flow like this:
1.
book_event.php will insert the
booking and then redirect to a booking_report.php page.
2.
booking_report.php will show a full
report based on the booking ID passed.
Step 1️⃣ : Updated book_event.php
<?php
session_start();
include('../db_connect.php');
if (!isset($_SESSION['user_id']))
{
die('You must log in to book an event.');
}
if (!isset($_GET['id']))
{
die('Event ID not provided.');
}
$user_id = $_SESSION['user_id'];
$event_id = $_GET['id'];
$booking_date = date('Y-m-d');
$status = 'Booked';
// Fetch Event Price
$event_query = "SELECT
price FROM events_table WHERE id = $event_id";
$event_result = mysqli_query($conn,
$event_query);
if (!$event_result || mysqli_num_rows($event_result)
== 0) {
die('Event not found.');
}
$event_row = mysqli_fetch_assoc($event_result);
$event_price = $event_row['price'];
// Generate Auto Token
Number
$token_number = uniqid('EVT-');
// Insert into
bookings_table
$query = "INSERT
INTO bookings_table (event_id, user_id, booking_date, status, price,
token_number)
VALUES ($event_id, $user_id, '$booking_date',
'$status', $event_price, '$token_number')";
if (mysqli_query($conn, $query))
{
$booking_id = mysqli_insert_id($conn); //
Get the booking ID
header("Location:
booking_report.php?id=$booking_id"); // Redirect to report page
exit();
} else {
echo "Error: " . mysqli_error($conn);
}
?>
Step 2️⃣ : booking_report.php
<?php
session_start();
include('../db_connect.php');
if (!isset($_SESSION['user_id']))
{
die('You must log in to view the booking
report.');
}
if (!isset($_GET['id']))
{
die('Booking ID not provided.');
}
$booking_id = $_GET['id'];
// Fetch Booking Details
$query = "SELECT
b.*, e.event_name
FROM bookings_table b
JOIN events_table e ON b.event_id =
e.id
WHERE b.id = $booking_id AND
b.user_id = {$_SESSION['user_id']}";
$result = mysqli_query($conn,
$query);
if (!$result || mysqli_num_rows($result)
== 0) {
die('Booking not found or you are not
authorized to view this booking.');
}
$booking = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Booking Report</title>
<style>
table {
border-collapse: collapse;
width: 50%;
margin: auto;
margin-top: 50px;
font-family: Arial, sans-serif;
}
th, td {
padding: 12px;
border: 1px solid #333;
}
th {
background-color: #f2f2f2;
}
h2 {
text-align: center;
margin-top: 30px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<h2>Booking
Report</h2>
<table>
<tr>
<th>Booking ID</th>
<td><?php echo $booking['id'];
?></td>
</tr>
<tr>
<th>User ID</th>
<td><?php echo $booking['user_id'];
?></td>
</tr>
<tr>
<th>Event Name</th>
<td><?php echo htmlspecialchars($booking['event_name']);
?></td>
</tr>
<tr>
<th>Booking Date</th>
<td><?php echo $booking['booking_date'];
?></td>
</tr>
<tr>
<th>Status</th>
<td><?php echo $booking['status'];
?></td>
</tr>
<tr>
<th>Price</th>
<td>₹ <?php echo number_format($booking['price'],
2); ?></td>
</tr>
<tr>
<th>Token Number</th>
<td><?php echo $booking['token_number'];
?></td>
</tr>
</table>
<br><br>
<div style="text-align:
center;">
<a href="my_bookings.php">🔙 Back to My
Bookings</a>
</div>
</body>
</html>
✅ Now the full process is:
Action |
Result |
Book from book_event.php |
✔ Booking is inserted |
Redirects automatically |
✔ Goes to booking_report.php?id=BookingID |
booking_report.php
shows |
✔ Full details of the booking |
🔥 Important Reminders
- Ensure
bookings_table has these fields:
id, event_id, user_id, booking_date, status, price, token_number - events_table
must have at least:
id, event_name, price