-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_flights.php
More file actions
104 lines (90 loc) · 3.62 KB
/
load_flights.php
File metadata and controls
104 lines (90 loc) · 3.62 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
session_start();
header('Content-Type: application/json');
require_once 'db_config.php';
// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
echo json_encode(['success' => false, 'message' => 'Please login to load flights.']);
exit;
}
// Check if user is admin (phone: 222-222-2222)
$userPhone = $_SESSION['phone'] ?? '';
if ($userPhone !== '222-222-2222') {
echo json_encode(['success' => false, 'message' => 'Only admin can load flights.']);
exit;
}
// Read flights.json file
$jsonFile = 'flights.json';
if (!file_exists($jsonFile)) {
echo json_encode(['success' => false, 'message' => 'flights.json file not found.']);
exit;
}
$jsonData = file_get_contents($jsonFile);
$flights = json_decode($jsonData, true);
if (!$flights || !is_array($flights)) {
echo json_encode(['success' => false, 'message' => 'Invalid JSON format in flights.json.']);
exit;
}
try {
// Clear existing flights (optional - you might want to keep existing ones)
// $conn->exec("TRUNCATE TABLE flights");
// Prepare insert statement
$stmt = $conn->prepare("
INSERT INTO flights (flight_id, origin, destination, departure_date, arrival_date, departure_time, arrival_time, available_seats, price)
VALUES (:flight_id, :origin, :destination, :departure_date, :arrival_date, :departure_time, :arrival_time, :available_seats, :price)
ON DUPLICATE KEY UPDATE
origin = VALUES(origin),
destination = VALUES(destination),
departure_date = VALUES(departure_date),
arrival_date = VALUES(arrival_date),
departure_time = VALUES(departure_time),
arrival_time = VALUES(arrival_time),
available_seats = VALUES(available_seats),
price = VALUES(price)
");
$loaded = 0;
$errors = [];
foreach ($flights as $flight) {
// Map JSON fields to database fields
$flightId = $flight['flightId'] ?? '';
$origin = $flight['origin'] ?? '';
$destination = $flight['destination'] ?? '';
$departureDate = $flight['departureDate'] ?? '';
$arrivalDate = $flight['arrivalDate'] ?? '';
$departureTime = $flight['departureTime'] ?? '';
$arrivalTime = $flight['arrivalTime'] ?? '';
$availableSeats = $flight['availableSeats'] ?? 0;
$price = $flight['price'] ?? 0;
// Validate required fields
if (empty($flightId) || empty($origin) || empty($destination)) {
$errors[] = "Skipping flight with missing required fields: " . json_encode($flight);
continue;
}
try {
$stmt->execute([
':flight_id' => $flightId,
':origin' => $origin,
':destination' => $destination,
':departure_date' => $departureDate,
':arrival_date' => $arrivalDate,
':departure_time' => $departureTime,
':arrival_time' => $arrivalTime,
':available_seats' => (int)$availableSeats,
':price' => (float)$price
]);
$loaded++;
} catch (PDOException $e) {
$errors[] = "Error inserting flight $flightId: " . $e->getMessage();
}
}
echo json_encode([
'success' => true,
'message' => "Successfully loaded $loaded flights into database.",
'loaded' => $loaded,
'total' => count($flights),
'errors' => $errors
]);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>