-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrips.php
More file actions
230 lines (200 loc) · 7.5 KB
/
Copy pathtrips.php
File metadata and controls
230 lines (200 loc) · 7.5 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<html>
<head>
<title>Trip Management</title>
</head>
<body>
<?php
//This is where our database connection is created, it lives under the variable $conn
include 'header.php';
if (isset($_POST['submit'])) {
openconnection();
$querystring="INSERT INTO trip (date,departure_time)
VALUES ('".$_POST['date']."', '".$_POST['deptime']."');";
//We don't really need to store the result for inserts/deletes, but it helps with debugging
$result=dbquery($querystring);
$id = $conn->insert_id;
//Update our relation tables
$querystring="INSERT INTO with_car VALUES ('".$_POST['car']."','".$id."');";
$result = dbquery($querystring);
$querystring="INSERT INTO to_loc VALUES ('".$_POST['destination']."','".$id."');";
$result = dbquery($querystring);
$querystring="INSERT INTO takes VALUES ('".$_POST['renter']."','".$id."');";
$result = dbquery($querystring);
//set the car to reserved while on trip
$querystring="UPDATE cars SET reserved = 1 WHERE cars.vin = ".$_POST['car'].";";
$result = dbquery($querystring);
closeconnection();
/*$example = $_POST['vin'];
$example2 = $_POST['make'];
echo $example . " " . $example2;
echo "Entry Added.\n";
echo $querystring."\n";
echo $result;*/
}
if(isset($_POST['remove'])) {
if(is_numeric($_POST['remove_id'])){
openconnection();
$querystring = "DELETE FROM trip WHERE trip_id='".$_POST['remove_id']."';";
//We don't really need to store the result for inserts/deletes, but it helps with debugging
$result = dbquery($querystring);
//set the car to not reserved because trip was deleted
$querystring="UPDATE cars SET reserved = 0 WHERE cars.vin = (SELECT vin FROM with_car WHERE trip_id='".$_POST['remove_id']."');";
$result = dbquery($querystring);
//Update our relation tables
$querystring = "DELETE FROM with_car WHERE trip_id='".$_POST['remove_id']."';";
$result = dbquery($querystring);
$querystring = "DELETE FROM to_loc WHERE trip_id='".$_POST['remove_id']."';";
$result = dbquery($querystring);
$querystring = "DELETE FROM takes WHERE trip_id='".$_POST['remove_id']."';";
$result = dbquery($querystring);
closeconnection();
}else
echo "<p style='color:red'>Remove ID has to be an integer</p>";
}
?>
<table style="width:100%">
<tr>
<td>
<form action="" method="post">
<br><br>
Starting Location (please select before entering the rest):<br>
<select name="location_choice">
<?php
openconnection();
$sql = "SELECT * FROM location;";
$result = dbquery($sql);
closeconnection();
//This little tidbit right here will loop through the query and print a new row in the table with it's info.
while($resultarray = mysqli_fetch_array($result)){
$selected = '';
if(isset($_POST['locationset'])) {
if($resultarray['location_id'] == $_POST['location_choice']) { $selected = " selected"; }
}
echo '<option value="'.$resultarray['location_id'].'"'.$selected.'>'.$resultarray['city'].'</option>';
}
?>
</select>
<input type="submit" value="Load cars at location" name="locationset">
</form>
<form action="" method="post">
Date (yyyy-mm-dd):<br>
<input type="text" name="date">
<br>
Departure Time (hh:mm:ss):<br>
<input type="text" name="deptime">
<br>
Car(VIN, Model):<br>
<select name="car">
<?php
if(isset($_POST['locationset'])) {
openconnection();
$sql = "select butts.vin, butts.city from
(select is_at.vin, location.location_id, location.city, location.state from is_at inner join location on is_at.location_id = location.location_id) butts
where butts.location_id ='".$_POST['location_choice']."';";
$result = dbquery($sql);
//$resultarray = mysqli_fetch_array($result);
//echo $sql;
//$tmpquery = "select cars.model, cars.vin from cars where cars.vin = '".$resultarray['vin']."';";
//$carresult = dbquery($tmpquery);
//This little tidbit right here will loop through the query and print a new row in the table with it's info.
while($resultarray = mysqli_fetch_array($result)){
$tmpquery = "select cars.model, cars.vin, cars.reserved from cars where cars.vin = '".$resultarray['vin']."';";
$carresult = dbquery($tmpquery);
$cararray = mysqli_fetch_array($carresult);
if($cararray['reserved'] == 0)
echo '<option value="'.$cararray['vin'].'">'.$cararray['vin'].','.$cararray['model'].'</option>';
}
closeconnection();
}
?>
</select>
<br>
Destination:<br>
<select name="destination">
<?php
openconnection();
$sql = "SELECT * FROM location;";
$result = dbquery($sql);
closeconnection();
//This little tidbit right here will loop through the query and print a new row in the table with it's info.
while($resultarray = mysqli_fetch_array($result)){
echo '<option value="'.$resultarray['location_id'].'">'.$resultarray['city'].'</option>';
}
?>
</select>
<br>
Name:<br>
<select name="renter">
<?php
openconnection();
$sql = "SELECT * FROM renter;";
$result = dbquery($sql);
closeconnection();
//This little tidbit right here will loop through the query and print a new row in the table with it's info.
while($resultarray = mysqli_fetch_array($result)){
echo '<option value="'.$resultarray['renter_id'].'">'.$resultarray['name'].'</option>';
}
?>
</select>
<br><br>
<input type="submit" value="Add Trip" name="submit" onclick="submitForms()">
</form>
</td>
<td>
<table border=1>
<tr>
<th>ID</th>
<th>Date</th>
<th>Departure Time</th>
<th>Destination</th>
<th>Car ID</th>
<th>Renter ID</th>
</tr>
<?php
openconnection();
$sql = "SELECT * FROM trip;";
$result = dbquery($sql);
//This little tidbit right here will loop through the query and print a new row in the table with it's info.
while($resultarray = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>"; print($resultarray['trip_id']); echo "</td>";
echo "<td>"; print($resultarray['date']); echo "</td>";
echo "<td>"; print($resultarray['departure_time']); echo "</td>";
$query = "select butts.city from
(select to_loc.trip_id, location.location_id, location.city, location.state from to_loc inner join location on to_loc.location_id = location.location_id) butts
where butts.trip_id ='".$resultarray['trip_id']."';";
$locationresult = dbquery($query);
//echo $query;
$locarray = mysqli_fetch_array($locationresult);
echo "<td>"; print($locarray['city']); echo "</td>";
$query = "select butts.vin from
(select with_car.trip_id, cars.vin from with_car inner join cars on with_car.vin = cars.vin) butts
where butts.trip_id ='".$resultarray['trip_id']."';";
$locationresult = dbquery($query);
//echo $query;
$locarray = mysqli_fetch_array($locationresult);
echo "<td>"; print($locarray['vin']); echo "</td>";
$query = "select butts.renter_id from
(select takes.trip_id, renter.renter_id from takes inner join renter on takes.renter_id = renter.renter_id) butts
where butts.trip_id ='".$resultarray['trip_id']."';";
$locationresult = dbquery($query);
//echo $query;
$locarray = mysqli_fetch_array($locationresult);
echo "<td>"; print($locarray['renter_id']); echo "</td>";
echo "</tr>";
}
closeconnection();
?>
</tr>
</table>
</td>
<td>
<form action="" method="post">
ID to Remove: <input type="text" name="remove_id">
<input type="submit" value="Remove" name="remove">
</form>
</td>
</tr>
</table>
</body>
</html>