-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdateMedal.php
232 lines (179 loc) · 7.43 KB
/
updateMedal.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
<!DOCTYPE html>
<html>
<head>
<br />
<title>UpdateMedals Page</title>
<style>
body {
background-image: url("mainBG.png");
background-repeat: no-repeat, repeat;
margin: 10px 30px;
}
p {font-family: Gill Sans; font-size: 20px;}
TD{font-family: Gill Sans; font-size: 18px;
background-color:#bee6ff; border: 2px solid #ddd; padding: 6px;}
Th{font-family: Gill Sans; font-size: 18px;
background-color:#bee6ff; border: 2px solid #ddd; padding: 6px;}
</style>
</head>
<body>
<b style="font-family:cursive; font-size:60px">Update Medals Operating Deck</b>
<br /><br /><br />
<b style="font-family:Gill Sans; font-size:20px;">Hi, visitor! Please choose the operations as follows!</b>
<br /><br /><br />
<form style="font-family:Gill Sans; font-size:20px;"
method="POST" action="updateMedal.php">
<input type="hidden" id="updateMedal" name="updateMedal">
Athlete's ID: <input type="text" name="athleteID"><br /><br />
New medal number: <input type="text" name="newNum"> (>=0)<br /><br />
<select style="font-family:Gill Sans; font-size:16px;" name="medalTypes" >MedalTypes
<option value="Choose">-- Choose Medal Types --</option>
<option value="Gold">Gold Medal</option>
<option value="Silver">Silver Medal</option>
<option value="Bronze">Bronze Medal</option>
</select>
<input style="font-family:Gill Sans; font-size:18px;"
type="submit" value="Submit" name="UpdateSubmit"></p>
</form>
<br />
<form method="GET" action="https://www.students.cs.ubc.ca/~lzy0606/mainPage.php">
<input style="font-family:Gill Sans; font-size:18px;"
type="submit" value="Back" name="Back"></p>
</form>
<?php
$success = True;
$db_conn = NULL;
$show_debug_alert_messages = False;
function debugAlertMessage($message)
{
global $show_debug_alert_messages;
if ($show_debug_alert_messages) {
echo "<script type='text/javascript'>alert('" . $message . "');</script>";
}
}
function executePlainSQL($cmdstr)
{ global $db_conn, $success;
$statement = OCIParse($db_conn, $cmdstr);
if (!$statement) {
echo "<br>Cannot parse the following command: " . $cmdstr . "<br>";
$e = OCI_Error($db_conn);
echo htmlentities($e['message']);
$success = False;
}
$r = OCIExecute($statement, OCI_DEFAULT);
if (!$r) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$e = oci_error($statement);
echo htmlentities($e['message']);
$success = False;
}
return $statement;
}
function printResult($result)
{
$current = OCI_Fetch_Array($result, OCI_BOTH);
if ($current[0] == NULL){
echo "<font color='green' face='Bradley Hand' size='5'><br /> Sorry! No such Competition Events.</font>";
} else {
echo "<table>";
echo "<tr><th>ID</th> <th>GoldNumber</th>
<th>SilverNumber</th> <th>BronzeNumber</th></tr>";
echo "<tr><td>" . $current["ID"] . "</td> <td>" . $current["GOLDNUMBER"] . "</td> <td>" . $current["SILVERNUMBER"] . "</td> <td>" . $current["BRONZENUMBER"] . "</td></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row["ID"] . "</td> <td>" . $row["GOLDNUMBER"] . "</td> <td>" . $row["SILVERNUMBER"] . "</td> <td>" . $row["BRONZENUMBER"] . "</td></tr>";
}
echo "</table>";
}
}
function connectToDB()
{
global $db_conn;
$db_conn = OCILogon("ora_lzy0606", "a71133417", "dbhost.students.cs.ubc.ca:1522/stu");
if ($db_conn) {
debugAlertMessage("Database is Connected");
return true;
} else {
debugAlertMessage("Cannot connect to Database");
$e = OCI_Error(); // For OCILogon errors pass no handle
echo htmlentities($e['message']);
return false;
}
}
function disconnectFromDB()
{
global $db_conn;
debugAlertMessage("Disconnect from Database");
OCILogoff($db_conn);
}
function handleUpdateRequest()
{
global $db_conn;
$chosenAthleteID = $_POST['athleteID'];
$num = $_POST['newNum'];
$modelFunction = $_POST['medalTypes'];
if (is_numeric($num) == 1 && intval($num)>=0 ){
$chosenNewNum = intval($num);
$check = executePlainSQL("SELECT ID
FROM Athlete
WHERE ID = '" . $chosenAthleteID . "'");
if (OCI_Fetch_Array($check, OCI_BOTH)[0] == NULL){
echo "<font color='red' face='Bradley Hand' size='5'><br /> Warning: Invalid athlete ID. Please try again.</font>";
}
else {
if ($modelFunction == 'Choose'){
echo "<font color='red' face='Bradley Hand' size='5'><br /> Warning: Please choose a medal type.</font>";
} else {
$before = executePlainSQL("SELECT *
FROM Athlete
WHERE ID = '" . $chosenAthleteID . "'");
if ($before != false) {
echo "<font color='green' face='Bradley Hand' size='5'>Before updating: <br /></font>";
printResult($before);
}
if ($modelFunction == 'Gold'){
$plan = "UPDATE Athlete
SET GoldNumber = $chosenNewNum
WHERE ID = '" . $chosenAthleteID . "'";
executePlainSQL($plan);
$change = true;
} else if ($modelFunction == 'Silver'){
$plan = "UPDATE Athlete
SET SilverNumber = $chosenNewNum
WHERE ID = '" . $chosenAthleteID . "'";
executePlainSQL($plan);
$change = true;
} else if ($modelFunction == 'Bronze'){
$plan = "UPDATE Athlete
SET BronzeNumber = $chosenNewNum
WHERE ID = '" . $chosenAthleteID . "'";
executePlainSQL($plan);
$change = true;
}
$after = executePlainSQL("SELECT *
FROM Athlete
WHERE ID = '" . $chosenAthleteID . "'");
if ($change == true) {
echo "<font color='green' face='Bradley Hand' size='5'><br />After updating: <br /></font>";
printResult($after);
}
}}
} else {
echo "<font color='red' face='Bradley Hand' size='5'><br /> Warning: Invalid number. Please try again.</font>";
}
OCICommit($db_conn);
}
function handlePOSTRequest()
{
if (connectToDB()) {
if (array_key_exists('UpdateSubmit', $_POST)) {
handleUpdateRequest();
}
disconnectFromDB();
}
}
if (isset($_POST['updateMedal']) ) {
handlePOSTRequest();
}
?>
<body>
<html>