-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartrek.php
503 lines (443 loc) · 15.5 KB
/
startrek.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
?>
<?php
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("oniddb.cws.oregonstate.edu","cawleys-db","N8zPsvQQLymkGPe8","cawleys-db");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body>
<h1>Star Trek The DataBase</h1>
<?php
//shows the first and last name of all characters in the database
echo'
<fieldset>
<legend>List of characters</legend>
<table>
<tr>
<th>First Name</th>
<th>Last name</th>
</tr>';
//calls first and last names from star_character
if(!($stmt = $mysqli->prepare("SELECT first_name AS 'First Name',
last_name AS 'Last Name'
FROM star_character;"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($fName, $lName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr>\n
<td>\n" . $fName . "\n</td>\n
<td>\n" . $lName . "\n</td>\n
</tr>";
}
$stmt->close();
echo'
</table>
</fieldset>
<br>';
//drop down menu with character names. when character is chosen will show:
//first name, last name, birth planet, starting rank, ending rank
//ships character has served on, and series character has appeared on
echo'
<fieldset>
<legend>View details about your favorite character</legend>
<form method="post" action="startrek.php">
<select name="Ranking">';
//pull in name and id to create drop down menu
if(!($stmt = $mysqli->prepare("SELECT character_ID, first_name, last_name FROM star_character"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($character_ID, $fname, $lname)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $character_ID . ' "> ' . $fname . " " . $lname .'</option>\n';
}
echo '</select>';
$stmt->close();
//submit button
echo '<p><input type="submit" value="Find Fave" name="Go!" /></p>
</form>';
// Check if submit button has been pressed. If so show information for selected character
if ( isset( $_POST['Go!'] ) ) {
//set return value from form
$Ranking=$_POST['Ranking'];
//pull in ranking information
if(!($stmt = $mysqli->prepare("SELECT first_name AS 'First Name',
last_name AS 'Last Name',
(select rank_name from rank where rank_id = start_rank) as 'Starting Rank',
(select rank_name from rank where rank_id = end_rank) as 'Ending Rank',
birth_place AS 'Birth Place'
FROM star_character
WHERE character_ID = " . $Ranking . ";"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($fName, $lName, $startingRank, $endingRank, $birthPlace)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "
<ul>\n Name: " . $fName . " " . $lName . "\n</ul>\n
<ul>\n Starting Rank: " . $startingRank . "\n</ul>\n
<ul>\n Ending Rank: " . $endingRank . "\n</ul>\n
<ul>\n Birth Planet: " . $birthPlace . "\n</ul>\n";
}
$stmt->close();
//get ships served on information
echo "<ul>Ships served on: ";
if(!($values = $mysqli->prepare("SELECT ship_name FROM ship
INNER JOIN ship_character ON ship_character.ship_ID = ship.ship_ID
WHERE ship_character.character_ID = $Ranking ;"))){
echo "Prepare failed: " . $values->errno . " " . $values->error;
}
if(!$values->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$values->bind_result($shipName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($values->fetch()){
echo $shipName . ", ";
}
echo "</ul>";
//get series' appeared on information
echo "<ul>Series' appeared on: ";
if(!($values = $mysqli->prepare("SELECT series_name FROM series
INNER JOIN series_character ON series_character.series_ID = series.series_ID
WHERE series_character.character_ID = $Ranking ;"))){
echo "Prepare failed: " . $values->errno . " " . $values->error;
}
if(!$values->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$values->bind_result($seriesName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($values->fetch()){
echo $seriesName . ", ";
}
echo "</ul>";
}
echo'
</fieldset>';
//allows users to add new character. Will only add first name, last name
//and birthplace. other connections will be made later
echo'
<br>
<fieldset>
<legend>Add a new character</legend>
<form method="post" action="startrek.php">';
//input character first name
echo '<p>First Name: <INPUT TYPE = "Text" NAME = "firstName"></p> ';
//input character last name
echo' <p>Last Name: <INPUT TYPE = "Text" NAME = "lastName"> </p>';
//input birthplace
echo '<p>Birth Planet: <INPUT TYPE = "Text" NAME = "birthPlanet"> </p> ';
//submit button
echo '<p><input type="submit" value = "Add!" name="Add" /></p>
</form>';
//check if button has been clicked
if ( isset( $_POST['Add'] ) ) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$homePlanet = $_POST['birthPlanet'];
//insert first, last, birthplace
if(!($stmt = $mysqli->prepare("INSERT INTO star_character(first_name, last_name, birth_place)
VALUES ('$firstName','$lastName' , '$homePlanet');"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "<br>Added " . $stmt->affected_rows . " row: " . $firstName . " " . $lastName .
"<br>To view changes please refresh the page" ;
}
$stmt->close();
}
echo'
</fieldset>';
echo'
<fieldset>
<legend>Add more information to a character</legend>
<form method = "post" action="startrek.php">';
//character drop down
echo
"Choose the character you would like to update: <select name='person'>";
//pull in name and id to create drop down menu
if(!($stmt = $mysqli->prepare("SELECT character_ID, first_name, last_name FROM star_character"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($character_ID, $fname, $lname)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $character_ID . ' "> ' . $fname . " " . $lname .'</option>\n';
}
echo '</select>';
$stmt->close();
//pull down for series
echo
'<p>Choose Series: <select name="chooseSeries"></p>';
//get series names
if(!($stmt = $mysqli->prepare("SELECT series_ID, series_name FROM series"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($series_ID, $seriesName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $series_ID . ' "> ' . $seriesName.'</option>\n';
}
echo'
<option value = -1>Other</option>\n
</select>';
$stmt->close();
echo'<p>Choose Ship: <select name="chooseShip"></p>';
//pull down for ship names
if(!($stmt = $mysqli->prepare("SELECT ship_ID, ship_name FROM ship"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($ship_ID, $shipName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $ship_ID . ' "> ' . $shipName.'</option>\n';
}
echo'
<option value = -1>Other</option>\n
</select>';
$stmt->close();
//dropdown for starting rank
echo '<p>Starting Rank: <select name="newStartRanking"></p>';
if(!($stmt = $mysqli->prepare("SELECT rank_ID, rank_name FROM rank"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($rank_ID, $rankName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $rank_ID . ' "> ' . $rankName.'</option>\n';
}
echo'
<option value = -1>Other</option>\n
</select>';
$stmt->close();
//dropdown for ending rank
echo '<p>Ending Rank: <select name="newEndRanking"></p>';
if(!($stmt = $mysqli->prepare("SELECT rank_ID, rank_name FROM rank"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($rank_ID, $rankName)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $rank_ID . ' "> ' . $rankName.'</option>\n';
}
echo'
<option value = -1>Other</option>\n
</select>';
$stmt->close();
echo '<br><p><input type="submit" value = "Update Information!" name="updateInfo" /></p>
</form>';
//when button is pressed
if (isset($_POST['updateInfo'])){
$character = $_POST['person'];
$series = $_POST['chooseSeries'];
$ship = $_POST['chooseShip'];
$startingRank = $_POST['newStartRanking'];
$endingRank = $_POST['newEndRanking'];
//add to series_character
if($series == -1 ){
echo'Please enter new Series below then add it to your character!<br>';
}
else{
if(!($stmt = $mysqli->prepare("INSERT INTO series_character(character_ID, series_ID)
VALUES ('$character', '$series')"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "<br>Added " . $stmt->affected_rows . " row: " .
"<br>To view changes please refresh the page" ;
}
$stmt->close();
}
//add ship to character
if($ship == -1){
echo'Please enter new Ship below then add it to your character!<br>';
}
else{
//add to ship_character
if(!($stmt = $mysqli->prepare("INSERT INTO ship_character(character_ID, ship_ID)
VALUES ('$character', '$ship')"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "<br>Added " . $stmt->affected_rows . " row: " .
"<br>To view changes please refresh the page" ;
}
$stmt->close();
}
//update beginning rank of character
if($startingRank == -1){
echo "Please enter new rank below then add it to your character";
}
else{
$stmt = $mysqli->query("UPDATE star_character SET start_rank = $startingRank WHERE character_ID = $character");
}
//update ending rank of character
if($endingRank == -1){
echo "Please enter new rank below then add it to your character";
}
else{
$stmt = $mysqli->query("UPDATE star_character SET end_rank = $endingRank WHERE character_ID = $character");
}
}
?>
</fieldset>
<fieldset>
<legend>Add a new rank</legend>
<form method = "post" action="startrek.php">
<input type="text" name="addNewRank">
<input type="submit" value="Add New Rank" name="newRankButton" />
</form>
<?php
//when button has been pressed
if(isset($_POST['newRankButton'])){
$newRank = $_POST['addNewRank'];
if(!($stmt = $mysqli->prepare("INSERT INTO rank(rank_name)
VALUES ('$newRank')"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "<br>" . $newRank . "Added ";
}
$stmt->close();
}
?>
</fieldset>
<fieldset>
<legend>Add a new series</legend>
<form method = "post" action="startrek.php">
<input type="text" name="addNewSeries">
<input type="submit" value="Add New Series" name="newSeriesButton" />
</form>
<?php
//when button has been pressed
if(isset($_POST['newSeriesButton'])){
echo"THE BUTTON WAS PRESSED!!!";
$newSeries = $_POST['addNewSeries'];
if(!($stmt = $mysqli->prepare("INSERT INTO series(series_name)
VALUES ('$newSeries')"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "<br>" . $seriesName . "Added ";
}
$stmt->close();
}
?>
</fieldset>
<fieldset>
<legend>Add a new ship</legend>
<form method = "post" action="startrek.php">
<input type="text" name="addNewShip">
<input type="submit" value="Add New Ship" name="newShipButton" />
</form>
<?php
//check button has been pressed
if(isset($_POST['newShipButton'])){
echo"THE BUTTON WAS PRESSED!!!";
$newShip = $_POST['addNewShip'];
if(!($stmt = $mysqli->prepare("INSERT INTO ship(ship_name)
VALUES ('$newShip')"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "<br>" . $newShip . " added ";
}
$stmt->close();
}
?>
</fieldset>
<fieldset>
<legend>Delete a character</legend>
<form method="post" action="startrek.php">
<?php
//character drop down
echo
"Choose the character you would like to delete: <select name='deleteCharacter'>";
//pull in name and id to create drop down menu
if(!($stmt = $mysqli->prepare("SELECT character_ID, first_name, last_name FROM star_character"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($character_ID, $fname, $lname)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
echo '<option>Choose</option>';
while($stmt->fetch()){
echo '<option value=" '. $character_ID . ' "> ' . $fname . " " . $lname .'</option>\n';
}
echo '</select>';
$stmt->close();
echo '<input type="submit" value="Delete Character" name="deleteCharacterButton" />';
//if button has been pressed
if (isset($_POST['deleteCharacterButton'])){
//delete character
$stmt = $mysqli->prepare("DELETE FROM star_character WHERE character_ID = ?");
$stmt->bind_param('i', $_POST['deleteCharacter']);
$stmt->execute();
$stmt->close();
}
echo '</fieldset>';