-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoracle-template.php
executable file
·248 lines (207 loc) · 6.72 KB
/
oracle-template.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
<?php
// The preceding tag tells the web server to parse the following text as PHP
// rather than HTML (the default)
// The following 3 lines allow PHP errors to be displayed along with the page
// content. Delete or comment out this block when it's no longer needed.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<html>
<head>
<title>CPSC 304 PHP/Oracle Demonstration</title>
</head>
<body>
<h2>Reset</h2>
<p>If you wish to reset the table press on the reset button. If this is the first time you're running this page, you MUST use reset</p>
<form method="POST" action="oracle-template.php">
<input type="hidden" id="resetTablesRequest" name="resetTablesRequest">
<p><input type="submit" value="Reset" name="reset"></p>
</form>
<hr />
<h2>Insert Values into DemoTable</h2>
<form method="POST" action="oracle-template.php">
<input type="hidden" id="insertQueryRequest" name="insertQueryRequest">
Number: <input type="text" name="insNo"> <br /><br />
Name: <input type="text" name="insName"> <br /><br />
<input type="submit" value="Insert" name="insertSubmit"></p>
</form>
<hr />
<h2>Update Name in DemoTable</h2>
<p>The values are case-sensitive and if you enter the wrong case, the update statement will not do anything.</p>
<form method="POST" action="oracle-template.php">
<input type="hidden" id="updateQueryRequest" name="updateQueryRequest">
Old Name: <input type="text" name="oldName"> <br /><br />
New Name: <input type="text" name="newName"> <br /><br />
<input type="submit" value="Update" name="updateSubmit"></p>
</form>
<hr />
<h2>Count the Tuples in DemoTable</h2>
<form method="GET" action="oracle-template.php">
<input type="hidden" id="countTupleRequest" name="countTupleRequest">
<input type="submit" name="countTuples"></p>
</form>
<hr />
<h2>Display Tuples in DemoTable</h2>
<form method="GET" action="oracle-template.php">
<input type="hidden" id="displayTuplesRequest" name="displayTuplesRequest">
<input type="submit" name="displayTuples"></p>
</form>
<?php
$success = True;
$db_conn = 0;
$show_debug_alert_messages = False;
$config = array(
"dbserver" => "127.0.0.1",
"dbuser" => "root",
"dbpassword" => "",
"dbname" => "MealMate",
"port" => 3306
);
function debugAlertMessage($message) {
global $show_debug_alert_messages;
if ($show_debug_alert_messages) {
echo "<script type='text/javascript'>alert('" . $message . "');</script>";
}
}
function connectToDB() {
global $db_conn, $config;
$db_conn = mysqli_connect(
$config["dbserver"],
$config["dbuser"],
$config["dbpassword"],
$config["dbname"],
$config["port"]
);
if ($db_conn) {
debugAlertMessage("Database is Connected");
return $db_conn;
} else {
debugAlertMessage("Cannot connect to Database");
echo "Error: " . mysqli_connect_error();
return null;
}
}
function disconnectFromDB() {
global $db_conn;
debugAlertMessage("Disconnect from Database");
mysqli_close($db_conn);
}
function executePlainSQL($cmdstr) {
global $db_conn, $success;
$result = mysqli_query($db_conn, $cmdstr);
if (!$result) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$success = False;
echo mysqli_error($db_conn);
}
return $result;
}
function executeBoundSQL($cmdstr, $list) {
global $db_conn, $success;
$stmt = mysqli_prepare($db_conn, $cmdstr);
if (!$stmt) {
echo "<br>Cannot prepare the following command: " . $cmdstr . "<br>";
$success = False;
echo mysqli_error($db_conn);
return;
}
foreach ($list as $tuple) {
$params = array();
$types = "";
foreach ($tuple as $key => $val) {
$params[] = $val;
if (is_int($val)) {
$types .= "i";
} elseif (is_float($val)) {
$types .= "d";
} else {
$types .= "s";
}
}
mysqli_stmt_bind_param($stmt, $types, ...$params);
$result = mysqli_stmt_execute($stmt);
if (!$result) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$success = False;
echo mysqli_error($db_conn);
}
}
mysqli_stmt_close($stmt);
}
function printResult($result) {
echo "<br>Retrieved data from table demoTable:<br>";
echo "<table>";
echo "<tr><th>ID</th><th>Name</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["name"] . "</td></tr>";
}
echo "</table>";
}
function handleResetRequest() {
global $db_conn;
executePlainSQL("DROP TABLE IF EXISTS demoTable");
echo "<br>Creating new table<br>";
executePlainSQL("CREATE TABLE demoTable (id INT PRIMARY KEY, name VARCHAR(30))");
mysqli_commit($db_conn);
echo "Reset successful.";
}
function handleInsertRequest() {
global $db_conn;
$id = $_POST['insNo'];
$name = $_POST['insName'];
$query = "INSERT INTO demoTable (id, name) VALUES (?, ?)";
$list = array(array($id, $name));
executeBoundSQL($query, $list);
mysqli_commit($db_conn);
echo "Insertion successful: ID=$id, Name=$name.";
}
function handleUpdateRequest() {
global $db_conn;
$oldName = $_POST['oldName'];
$newName = $_POST['newName'];
$query = "UPDATE demoTable SET name=? WHERE name=?";
$list = array(array($newName, $oldName));
executeBoundSQL($query, $list);
mysqli_commit($db_conn);
echo "Update successful: Old Name=$oldName, New Name=$newName.";
}
function handleCountRequest() {
global $db_conn;
$result = executePlainSQL("SELECT COUNT(*) AS count FROM demoTable");
if ($row = mysqli_fetch_assoc($result)) {
echo "<br>The number of tuples in demoTable: " . $row['count'] . "<br>";
}
}
function handleDisplayRequest() {
global $db_conn;
$result = executePlainSQL("SELECT * FROM demoTable");
printResult($result);
}
function handlePOSTRequest() {
if (isset($_POST['resetTablesRequest'])) {
handleResetRequest();
} elseif (isset($_POST['insertQueryRequest'])) {
handleInsertRequest();
} elseif (isset($_POST['updateQueryRequest'])) {
handleUpdateRequest();
}
}
function handleGETRequest() {
if (isset($_GET['countTupleRequest'])) {
handleCountRequest();
} elseif (isset($_GET['displayTuplesRequest'])) {
handleDisplayRequest();
}
}
if (connectToDB()) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
handlePOSTRequest();
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
handleGETRequest();
}
disconnectFromDB();
}
?>
</body>
</html>