-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsert_page.php
executable file
·216 lines (174 loc) · 10.6 KB
/
Insert_page.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
<!--Test Oracle file for UBC CPSC304 2018 Winter Term 1
Created by Jiemin Zhang
Modified by Simona Radu
Modified by Jessica Wong (2018-06-22)
This file shows the very basics of how to execute PHP commands
on Oracle.
Specifically, it will drop a table, create a table, insert values
update values, and then query for values
IF YOU HAVE A TABLE CALLED "demoTable" IT WILL BE DESTROYED
The script assumes you already have a server set up
All OCI commands are commands to the Oracle libraries
To get the file to work, you must place it somewhere where your
Apache server can run it, and you must rename it to have a ".php"
extension. You must also change the username and password on the
OCILogon below to be your ORACLE username and password -->
<html>
<html>
<body>
<h1 assign="center" style="color:blue;font-size:40px"> CPSC 304 PHP/Oracle Demonstration</h1>
</body>
</html>
<body>
<img src="war_image.jpg" alt="war image" width="500" height="600">
</body>
<head>
<title>CPSC 304 PHP/Oracle Demonstration</title>
</head>
<style>
body {
background-image:url(data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxAQDQ0NDg0QDQ0NDQ0NDQ0NDQ8NDg0NFREWFhURExM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CpllM1ssF6Y+7YmNnKZJkHXUa3G2TkDtrZrcbZYO2s1uFlg//Z);
background-size: cover;
}
</style>
<body>
<h2>Adding single value to Combatant table</h2>
<form method="POST" action="Insert_page.php"> <!--refresh page when submitted-->
<input type="hidden" id="requestInsert" name="requestInsert">
CID: <input type="text" name="val0"> <br /><br />
Combatant_name: <input type="text" name="val1"> <br /><br />
HealthStatus: <input type="text" name="val2"> <br /><br />
Hometown: <input type="text" name="val3"> <br /><br />
Height: <input type="text" name="val4"> <br /><br />
Combatant_weight: <input type="text" name="val5"> <br /><br />
Age: <input type="text" name="val6"> <br /><br />
Service_year: <input type="text" name="val7"> <br /><br />
MUID: <input type="text" name="val8"> <br /><br />
<input type="submit" value="Insert Button" name="clickInsert"></p>
</form>
<h2>Result</h2>
<form method="POST" action="Insert_page.php"> <!--refresh page when submitted-->
<input type="submit" value="Display Result" name="display_result"></p>
</form>
<form method="POST" action="demo_page.php"> <!--refresh page when submitted-->
<input type="submit" value="BACK TO DEMO PAGE" name="DEMO_redirect"></p>
</form>
<hr />
<?php
//this tells the system that it's no longer just parsing html; it's now parsing PHP
$success = True; //keep track of errors so it redirects the page only if there are no errors
$db_conn = NULL; // edit the login credentials in connectToDB()
$show_debug_alert_messages = False; // set to True if you want alerts to show you which methods are being triggered (see how it is used in debugAlertMessage())
function debugAlertMessage($message) {
global $show_debug_alert_messages;
if ($show_debug_alert_messages) {
echo "<script type='text/javascript'>alert('" . $message . "');</script>";
}
}
function executePlainSQL($cmdstr) { //takes a plain (no bound variables) SQL command and executes it
//echo "<br>running ".$cmdstr."<br>";
global $db_conn, $success;
$statement = OCIParse($db_conn, $cmdstr);
//There are a set of comments at the end of the file that describe some of the OCI specific functions and how they work
if (!$statement) {
echo "<br>Cannot parse the following command: " . $cmdstr . "<br>";
$e = OCI_Error($db_conn); // For OCIParse errors pass the connection handle
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); // For OCIExecute errors pass the statementhandle
echo htmlentities($e['message']);
$success = False;
}
return $statement;
}
function executeBoundSQL($cmdstr, $list) {
/* Sometimes the same statement will be executed several times with different values for the variables involved in the query.
In this case you don't need to create the statement several times. Bound variables cause a statement to only be
parsed once and you can reuse the statement. This is also very useful in protecting against SQL injection.
See the sample code below for how this function is used */
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;
}
foreach ($list as $tuple) {
foreach ($tuple as $bind => $val) {
//echo $val;
//echo "<br>".$bind."<br>";
OCIBindByName($statement, $bind, $val);
unset ($val); //make sure you do not remove this. Otherwise $val will remain in an array object wrapper which will not be recognized by Oracle as a proper datatype
}
$r = OCIExecute($statement, OCI_DEFAULT);
if (!$r) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$e = OCI_Error($statement); // For OCIExecute errors, pass the statementhandle
echo htmlentities($e['message']);
echo "<br>";
$success = False;
}
}
}
function printResultCombatant($result) { //prints results from a select statement
echo "<br>Retrieved data from table Combatant:<br>";
echo "<table>";
echo "<tr><th>CID</th><th>Combatant_name</th><th>HealthStatus</th><th>Hometown</th><th>Height</th><th>Combatant_weight</th><th>Age</th><th>Service_year</th><th>MUID</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4]
. "</td><td>" . $row[5] . "</td><td>" . $row[6] . "</td><td>" . $row[7] . "</td><td>" . $row[8] . "</td><td>"; //or just use "echo $row[0]"
// echo $row[0];
}
echo "</table>";
}
function connectToDB() {
global $db_conn;
// Your username is ora_(CWL_ID) and the password is a(student number). For example,
// ora_platypus is the username and a12345678 is the password.
// $db_conn = OCILogon("ora_liu650", "a46981452", "dbhost.students.cs.ubc.ca:1522/stu");
// $db_conn = OCILogon("ora_maxonzz", "a57800732", "dbhost.students.cs.ubc.ca:1522/stu");
$db_conn = OCILogon("ora_mhlchina", "a28325181", "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 handleInsert() {
global $db_conn;
executePlainSQL("INSERT INTO Combatant values ({$_POST['val0']}, '{$_POST['val1']}', '{$_POST['val2']}', '{$_POST['val3']}',
{$_POST['val4']}, {$_POST['val5']}, {$_POST['val6']}, {$_POST['val7']}, {$_POST['val8']})");
OCICommit($db_conn);
}
if (isset($_POST['clickInsert'])) {
if(connectToDB()) {
handleInsert();
disconnectFromDB();
}
} else if (isset($_POST['display_result'])) {
if(connectToDB()) {
$result = executePlainSQL("SELECT * FROM Combatant");
printResultCombatant($result);
disconnectFromDB();
}
} else if (isset($_POST['DEMO_redirect'])) {
header('Location: https://www.students.cs.ubc.ca/~maxonzz/military-system/demo_page.php');
exit;
}
?>
</body>
</html>