-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateSeats.php
79 lines (69 loc) · 3.05 KB
/
updateSeats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
require 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$flightId = $_POST['flightId'];
$totalPassengers = $_POST['totalPassengers'];
$totalPrice = $_POST['totalPrice'];
$passengers = json_decode($_POST['passengers'], true);
$bookingNumber = $_POST['bookingNumber'];
try {
// Start transaction
$pdo->beginTransaction();
// Step 1: Check if the flight has enough available seats
$stmt = $pdo->prepare("SELECT available_seats FROM flights WHERE flight_id = :flightId");
$stmt->execute([':flightId' => $flightId]);
$flight = $stmt->fetch();
if (!$flight || $flight['available_seats'] < $totalPassengers) {
echo "Not enough available seats.";
$pdo->rollBack();
exit;
}
// Step 2: Update available seats in the flights table
$newAvailableSeats = $flight['available_seats'] - $totalPassengers;
$stmt = $pdo->prepare("UPDATE flights SET available_seats = :availableSeats WHERE flight_id = :flightId");
$stmt->execute([
':availableSeats' => $newAvailableSeats,
':flightId' => $flightId
]);
// Step 3: Insert flight booking into flight-booking table
$stmt = $pdo->prepare("INSERT INTO flight_bookings (flight_booking_id, flight_id, total_price)
VALUES (:bookingId, :flightId, :totalPrice)");
$stmt->execute([
':bookingId' => $bookingNumber,
':flightId' => $flightId,
':totalPrice' => $totalPrice
]);
// Step 4: Insert passengers into passengers table and tickets into tickets table
foreach ($passengers as $passenger) {
// Insert into passengers table (Ignore duplicates)
$stmt = $pdo->prepare("INSERT IGNORE INTO passengers (ssn, first_name, last_name, date_of_birth, category)
VALUES (:ssn, :firstName, :lastName, :dob, :category)");
$stmt->execute([
':ssn' => $passenger['ssn'],
':firstName' => $passenger['firstName'],
':lastName' => $passenger['lastName'],
':dob' => $passenger['dob'],
':category' => $passenger['category']
]);
// Insert into tickets table (Ignore duplicates)
$stmt = $pdo->prepare("INSERT IGNORE INTO tickets (ticket_id, flight_booking_id, ssn, price)
VALUES (:ticketId, :bookingId, :ssn, :price)");
$stmt->execute([
':ticketId' => $passenger['ticketID'],
':bookingId' => $bookingNumber,
':ssn' => $passenger['ssn'],
':price' => $totalPrice
]);
}
// Commit the transaction
$pdo->commit();
echo "Seats updated successfully, and booking saved!";
} catch (Exception $e) {
// Rollback on error
$pdo->rollBack();
echo "An error occurred: " . $e->getMessage();
}
} else {
echo "Invalid request.";
}
?>