-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.php
160 lines (150 loc) · 6.3 KB
/
add.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
session_start();
$pageTitle = 'Add Flights';
include 'init.php';
include $template . 'header.php';
include $template.'navbar.php';
if(!isset($_SESSION['user']) || $_SESSION['user']['role'] != 1) {
header('Location: login.php');
}
if($_SERVER['REQUEST_METHOD'] === "POST" && isset($_GET['aircraft'])){
$aircraftName = $_POST['aircraftName'];
$ecoSeats = $_POST['ecoSeats'];
$busSeats = $_POST['busSeats'];
$fSeats = $_POST['fSeats'];
$model = $_POST['model'];
$mxWeight = $_POST['mxWeight'];
$date = $_POST['date'];
$totalChairs = (int)$ecoSeats + (int)$busSeats + (int)$fSeats;
/*
* Insert Query
*
* */
$sql = "INSERT INTO aircraft(name, max_weight, number_of_seats, manufacture_date, model)
values('$aircraftName', '$mxWeight', '$totalChairs', '$date', '$model');";
$stmt = $conn->prepare($sql);
$stmt->execute();
$aircraftId = $conn->lastInsertId();
$sql = "INSERT INTO seat(aircraft_id, class_type) values";
for($i = 0; $i < $ecoSeats; $i++) {
$sql .= "('$aircraftId', 'Economic'),";
}
for($i = 0; $i < $busSeats; $i++) {
$sql.= "('$aircraftId', 'Business'),";
}
for($i = 0; $i < $fSeats; $i++) {
$sql .= "('$aircraftId', 'First Class'),";
}
// remove last comma
$sql = substr($sql, 0, -1);
$stmt = $conn->prepare($sql);
$stmt->execute();
header('Location: add.php');
}
if($_SERVER['REQUEST_METHOD'] === "POST" && isset($_GET['flight'])){
$sql = "INSERT INTO flight (aircraft_id, dep_location, arrival_location, dep_time,arrival_time, price, airline)
VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $_POST['aircraft_id']);
$stmt->bindParam(2, $_POST['dep_location']);
$stmt->bindParam(3, $_POST['arrival_location']);
$stmt->bindParam(4, $_POST['dep_time']);
$stmt->bindParam(5, $_POST['arrival_time']);
$stmt->bindParam(6, $_POST['price']);
$stmt->bindParam(7, $_POST['airline']);
$stmt->execute();
header('Location: add.php');
}
?>
<div class="row mx-auto container col-11 col-md-8 d-flex justify-content-center">
<div class="my-5 col-md-6 col-11">
<!--Edit Profile-->
<h3 class="text-center">Add Aircraft</h3>
<form class="form" action="add.php?aircraft=true" method="POST">
<div class="form-group my-4">
<label>Aircraft Name</label>
<input type="text" name="aircraftName" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>Model</label>
<input type="text" name="model" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>Economics Seats</label>
<input type="number" name="ecoSeats" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>Business Seats</label>
<input type="number" name="busSeats" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>First Class Seats</label>
<input type="number" name="fSeats" class="form-control">
</div>
<div class="form-group my-4 position-relative">
<label>Max Weight</label>
<input type="number" name="mxWeight" class="form-control">
</div>
<div class="form-group my-4">
<label>Manufacture Date</label>
<input type="date" name="date" class="form-control">
</div>
<div class="row justify-content-center">
<!-- <input type="hidden" name="user_id" value="--><?php //= $_SESSION['user'] ?><!--">-->
<button type="submit" class="btn button mt-4 w-md-50 w-75"> Add </button>
</div>
</form>
</div>
<div class="my-5 col-md-6 col-11">
<!--Edit Profile-->
<h3 class="text-center">Add Flight</h3>
<form class="form" action="add.php?flight=true" method="POST">
<div class="form-group my-4">
<?php
$sql = "SELECT * FROM aircraft";
$stmt = $conn->prepare($sql);
$stmt->execute();
$aircrafts = $stmt->fetchAll();
?>
Select aircraft
<select class="form-select" name="aircraft_id">
<?php
foreach ($aircrafts as $aircraft) {
echo "<option value='" . $aircraft['aircraft_id'] . "'>" . $aircraft['name'] . "</option>";
}
?>
</select>
</div>
<div class="form-group my-4">
<label>Departure Location</label>
<input type="text" name="dep_location" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>Arrival Location</label>
<input type="text" name="arrival_location" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>Departure Time</label>
<input type="datetime-local" name="dep_time" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4">
<label>Arrival Time</label>
<input type="datetime-local" name="arrival_time" class="form-control" aria-describedby="">
</div>
<div class="form-group my-4 position-relative">
<label>Flight Price</label>
<input type="number" name="price" class="form-control">
</div>
<div class="form-group my-4">
<label>Airline</label>
<input type="text" name="airline" class="form-control">
</div>
<div class="row justify-content-center">
<button type="submit" class="btn button mt-4 w-md-50 w-75"> Add </button>
</div>
</form>
</div>
</div>
<?php
include $template . 'footer.php';
?>