-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
713 lines (583 loc) · 31.6 KB
/
main.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<html>
<head>
<title>CPSC 304 PHP/Content Creator Database</title>
</head>
<body>
<h1>CPSC 304 PHP/Content Creator Database</h1>
<h2>Insert Content Creators into Table</h2>
<p>NOTE: A valid/existing ContractID, valid/existing combination of AgencyName and AgencyFounder, and UNIQUE Contact MUST be entered.</p>
<form method="POST" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="insertQueryRequest" name="insertQueryRequest">
Name: <input type="text" name="insName"> <br /><br />
Handle: <input type="text" name="insHandle"> <br /><br />
Contact: <input type="text" name="insContact"> <br /><br />
Status: <input type="text" name="insStatus"> <br /><br />
Agency Name: <input type="text" name="insAgencyName"> <br /><br />
Agency Founder: <input type="text" name="insAgencyFounder"> <br /><br />
ContractID: <input type="text" name="insContractID"> <br /><br />
<input type="submit" value="Insert" name="insertSubmit"></p>
</form>
<hr />
<h2>Delete Content Creators from Table</h2>
<p>NOTE: Any products sold from the deleted Content Creator will also be deleted.</p>
<form method="POST" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="deleteQueryRequest" name="deleteQueryRequest">
Content Creator Contact: <input type="text" name="delID"> <br /><br />
<input type="submit" value="Delete" name="deleteSubmit"></p>
</form>
<hr />
<h2>Update Attributes in ContentCreators Table</h2>
<p>NOTE: The values are case sensitive and if you enter in the wrong case, the update statement will not do anything.</p>
<form method="POST" action="main.php" id="atype"> <!--refresh page when submitted-->
<input type="hidden" id="updateQueryRequest" name="updateQueryRequest">
<label for="attributes">Choose an attribute to update:</label>
<select id="attributes" name="atype" form="atype">
<option value="content_creator_name">Name</option>
<option value="handle">Handle</option>
<option value="cStatus">Status</option>
<option value="contracts_id">ContractID</option>
</select> <br /><br />
Content Creator Contact: <input type="text" name="CCC"> <br /><br />
Old Value: <input type="text" name="oldValue"> <br /><br />
New Value: <input type="text" name="newValue"> <br /><br />
<input type="submit" value="Update" name="updateSubmit"></p>
</form>
<hr />
<h2>Select from Tables</h2>
<form method="POST" action="main.php">
<input type="hidden" id="selectQueryRequest" name="selectQueryRequest">
<label for="selectTable">Select a Table:</label>
<select id="selectTable" name="selectTable">
<option value="ContentCreators">ContentCreators</option>
<option value="Agencies">Agencies</option>
<option value="Contracts1">Contracts</option>
<option value="MerchandiseSold1">Merchandise</option>
<option value="MerchandiseSold2">Merchandise2</option>
<option value="Youtubers">Youtubers</option>
<option value="Offer">Offer</option>
</select><br /><br />
<label for="selectAttributes">Select Attribute(s):</label>
<select id="selectAttributes" name="selectAttributes[]" multiple>
<option value="content_creator_contact">Content Creator Contact</option>
<option value="cStatus">Content Creator Status</option>
<option value="content_creator_name">Content Creator Name</option>
<option value="handle">Content Creator Handle</option>
<option value="agency_name">Agency Name</option>
<option value="agency_founder">Agency Founder</option>
<option value="employee_count">Agency Employee Count</option>
<option value="aLocation">Agency Location</option>
<option value="contracts_id">Contract ID</option>
<option value="cDate">Contract Date</option>
<option value="pay_rate">Contract Pay</option>
<option value="itemNumber">Merchandise Number</option>
<option value="mType">Manufacturer Type</option>
<option value="manufacturer">Manufacturer</option>
<option value="price">Merch Price</option>
<option value="subscriber_count">Subscriber Count</option>
</select><br /><br />
<label for="selectCondition">Condition:</label>
<select id="selectConditionField" name="selectConditionField1">
<option value=""></option> <!-- Blank option -->
<option value="content_creator_contact">Content Creator Contact</option>
<option value="cStatus">Content Creator Status</option>
<option value="content_creator_name">Content Creator Name</option>
<option value="handle">Content Creator Handle</option>
<option value="agency_name">Agency Name</option>
<option value="agency_founder">Agency Founder</option>
<option value="employee_count">Agency Employee Count</option>
<option value="aLocation">Agency Location</option>
<option value="contracts_id">Contract ID</option>
<option value="cDate">Contract Date</option>
<option value="pay_rate">Contract Pay</option>
<option value="itemNumber">Merchandise Number</option>
<option value="mType">Merch Type</option>
<option value="manufacturer">Manufacturer</option>
<option value="price">Merch Price</option>
<option value="subscriber_count">Subscriber Count</option>
</select>
=
<input type="text" id="selectConditionValue" name="selectConditionValue1">
AND
<select id="selectConditionField" name="selectConditionField2">
<option value=""></option> <!-- Blank option -->
<option value="content_creator_contact">Content Creator Contact</option>
<option value="employee_count">Agency Employee Count</option>
<option value="contracts_id">Contract ID</option>
<option value="pay_rate">Contract Pay</option>
<option value="itemNumber">Merchandise Number</option>
<option value="price">Merch Price</option>
<option value="subscriber_count">Subscriber Count</option>
</select>
>
<input type="text" id="selectConditionValue" name="selectConditionValue2"> <br /><br />
<input type="submit" value="Select" name="selectSubmit">
</form>
<hr />
<h2>Projection from Creators Table</h2>
<form method="GET" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="projectionQueryRequest" name="projectionQueryRequest">
<label for="selectAttributes">Select Attribute(s):</label>
<select id="selectAttributes" name="selectAttributes[]" multiple>
<option value="content_creator_contact">Content Creator Contact</option>
<option value="cStatus">Content Creator Status</option>
<option value="content_creator_name">Content Creator Name</option>
<option value="handle">Content Creator Handle</option>
<option value="agency_name">Agency Name</option>
<option value="agency_founder">Agency Founder</option>
<option value="contracts_id">Contract ID</option>
</select><br /><br />
<input type="submit" value="Projection" name="projectionSubmit"></p>
</form>
<hr />
<h2>Join ContentCreators With MerchandiseSold1</h2>
<p>To find all products sold by a particular Content Creator</p>
<form method="POST" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="joinQueryRequest" name="joinQueryRequest">
Content Creator Contact: <input type="text" name="ccid"> <br /><br />
<input type="submit" value="Join" name="joinSubmit"></p>
</form>
<hr />
<h2>Aggregation with Group By from Creators Table</h2>
<p>To find the pay of the highest earning Content Creator from each Agency</p>
<form method="GET" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="aggregationWithGroupByQueryRequest" name="aggregationWithGroupByQueryRequest">
<input type="submit" value="Get" name="aggregationWithGroupBySubmit"></p>
</form>
<hr />
<h2>Aggregation with Having from Merchandise2 Table</h2>
<p>Aggregate by manufacturer and display its most expensive item if more than $30</p>
<form method="GET" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="aggregationWithHavingQueryRequest" name="aggregationWithHavingQueryRequest">
<input type="submit" value="AggregationWithHaving" name="aggregationWithHavingSubmit"></p>
</form>
<hr />
<h2>Nested Aggregation with Group By from Youtubers Table</h2>
<p>Find largest subscriber average from all agencies</p>
<form method="GET" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="nestedAggregationWithGroupByQueryRequest" name="nestedAggregationWithGroupByQueryRequest">
<input type="submit" value="NestedAggregationWithGroupBy" name="nestedAggregationWithGroupBySubmit"></p>
</form>
<hr />
<h2>Division from Agencies Table</h2>
<p>Find the agencies who can give all types of contracts</p>
<form method="GET" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="divisionQueryRequest" name="divisionQueryRequest">
<input type="submit" value="Division" name="divisionSubmit"></p>
</form>
<hr />
<h2>Show All Tables</h2>
<form method="GET" action="main.php"> <!--refresh page when submitted-->
<input type="hidden" id="showAllTablesRequest" name="showAllTablesRequest">
<input type="submit" value="Show" name="showAllTablesSubmit"></p>
</form>
<hr />
<h2>Query Results</h2>
<?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 printResultAll() {
// Print ContentCreators Table
$cc_table = executePlainSQL("SELECT * FROM ContentCreators");
echo "<br>ContentCreators Table:<br>";
echo "<table>";
echo "<tr><th>Contact</th><th>Status</th><th>Name</th><th>AgencyName</th><th>AgencyFounder</th><th>ContractID</th><th>Handle</th></tr>";
while ($row = OCI_Fetch_Array($cc_table, 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><tr>"; //or just use "echo $row[0]"
}
echo "</table>";
// Print Youtubers Table
$cc_table = executePlainSQL("SELECT * FROM Youtubers");
echo "<br>Youtubers Table:<br>";
echo "<table>";
echo "<tr><th>Contact</th><th>Subscriber_count</th></tr>";
while ($row = OCI_Fetch_Array($cc_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><tr>"; //or just use "echo $row[0]"
}
echo "</table>";
// Print Agencies Table
$agencies_table = executePlainSQL("SELECT * FROM Agencies");
echo "<br>Agencies Table:<br>";
echo "<table>";
echo "<tr><th>Name</th><th>Founder</th><th>EmployeeCount</th></tr>";
while ($row = OCI_Fetch_Array($agencies_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><tr>";
}
echo "</table>";
// Print Contracts1 Table
$contracts1_table = executePlainSQL("SELECT * FROM Contracts1");
echo "<br>Contracts1 Table:<br>";
echo "<table>";
echo "<tr><th>ID</th><th>Date</th><th>Pay</th></tr>";
while ($row = OCI_Fetch_Array($contracts1_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><tr>";
}
echo "</table>";
// Print Offer Table
$contracts1_table = executePlainSQL("SELECT * FROM Offer");
echo "<br>Offer Table:<br>";
echo "<table>";
echo "<tr><th>agency_name</th><th>agency_founder</th><th>contracts_id</th></tr>";
while ($row = OCI_Fetch_Array($contracts1_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><tr>";
}
echo "</table>";
// Print MerchandiseSold1 Table
$ms1_table = executePlainSQL("SELECT * FROM MerchandiseSold1");
echo "<br>MerchandiseSold1 Table:<br>";
echo "<table>";
echo "<tr><th>Item#</th><th>Type</th><th>Manufacturer</th><th>ContentCreatorContact</th></tr>";
while ($row = OCI_Fetch_Array($ms1_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><tr>";
}
echo "</table>";
// Print MerchandiseSold2 Table
$ms1_table = executePlainSQL("SELECT * FROM MerchandiseSold2");
echo "<br>MerchandiseSold2 Table:<br>";
echo "<table>";
echo "<tr><th>mType</th><th>Manufacturer</th><th>Manufacturer</th><th>price</th></tr>";
while ($row = OCI_Fetch_Array($ms1_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><tr>";
}
echo "</table>";
}
function connectToDB() {
global $db_conn;
$json = file_get_contents("oracle.json");
$json_data = json_decode($json,true);
// 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($json_data["oracle_username"], $json_data["oracle_password"], "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;
$form_type = $_POST['atype'];
$old_val = $_POST['oldValue'];
$new_val = $_POST['newValue'];
$cc_id = $_POST['CCC'];
executePlainSQL("UPDATE ContentCreators SET " . $form_type . "='" . $new_val . "' WHERE " . $form_type . "='" . $old_val . "' AND content_creator_contact='" . $cc_id . "'");
// Print ContentCreators Table
$cc_table = executePlainSQL("SELECT * FROM ContentCreators");
echo "<br>ContentCreators Table:<br>";
echo "<table>";
echo "<tr><th>Contact</th><th>Status</th><th>Name</th><th>AgencyName</th><th>AgencyFounder</th><th>ContractID</th><th>Handle</th></tr>";
while ($row = OCI_Fetch_Array($cc_table, 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><tr>"; //or just use "echo $row[0]"
}
echo "</table>";
OCICommit($db_conn);
}
function handleInsertRequest() {
global $db_conn;
//Getting the values from user and insert data into the table
$tuple = array (
":bind1" => $_POST['insContact'],
":bind2" => $_POST['insStatus'],
":bind3" => $_POST['insName'],
":bind4" => $_POST['insAgencyName'],
":bind5" => $_POST['insAgencyFounder'],
":bind6" => $_POST['insContractID'],
":bind7" => $_POST['insHandle']
);
$alltuples = array (
$tuple
);
executeBoundSQL("INSERT INTO ContentCreators VALUES (:bind1, :bind2, :bind3, :bind4, :bind5, :bind6, :bind7)", $alltuples);
// Print ContentCreators Table
$cc_table = executePlainSQL("SELECT * FROM ContentCreators");
echo "<br>ContentCreators Table:<br>";
echo "<table>";
echo "<tr><th>Contact</th><th>Status</th><th>Name</th><th>AgencyName</th><th>AgencyFounder</th><th>ContractID</th><th>Handle</th></tr>";
while ($row = OCI_Fetch_Array($cc_table, 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><tr>"; //or just use "echo $row[0]"
}
echo "</table>";
OCICommit($db_conn);
}
function handleShowAllTablesRequest() {
global $db_conn;
printResultAll();
}
function handleDeleteRequest() {
global $db_conn;
$del_id = $_POST['delID'];
executePlainSQL("DELETE FROM ContentCreators WHERE content_creator_contact=" . $del_id . "");
echo "<br>";
// Print ContentCreators Table
$cc_table = executePlainSQL("SELECT * FROM ContentCreators");
echo "<br>ContentCreators Table:<br>";
echo "<table>";
echo "<tr><th>Contact</th><th>Status</th><th>Name</th><th>AgencyName</th><th>AgencyFounder</th><th>ContractID</th><th>Handle</th></tr>";
while ($row = OCI_Fetch_Array($cc_table, 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><tr>"; //or just use "echo $row[0]"
}
echo "</table>";
// Print MerchandiseSold1 Table
$ms1_table = executePlainSQL("SELECT * FROM MerchandiseSold1");
echo "<br>MerchandiseSold1 Table:<br>";
echo "<table>";
echo "<tr><th>Item#</th><th>Type</th><th>Manufacturer</th><th>ContentCreatorContact</th></tr>";
while ($row = OCI_Fetch_Array($ms1_table, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><tr>";
}
echo "</table>";
OCICommit($db_conn);
}
function handleSelectRequest() {
global $db_conn;
$table = $_POST['selectTable'];
$attribute = $attribute = implode(', ', $_POST['selectAttributes']);
$c_field1 = $_POST['selectConditionField1'];
$c_value1 = $_POST['selectConditionValue1'];
$c_field2 = $_POST['selectConditionField2'];
$c_value2 = $_POST['selectConditionValue2'];
$query = "SELECT $attribute FROM $table WHERE $c_field1='$c_value1' AND $c_field2>'$c_value2'";
$result = executePlainSQL($query);
echo "<h3>Query Result:</h3>";
echo "<table border='1'>";
$field_names = oci_num_fields($result);
echo "<tr>";
for ($i = 1; $i <= $field_names; $i++) {
$field_name = oci_field_name($result, $i);
echo "<th>$field_name</th>";
}
echo "</tr>";
while ($row = oci_fetch_array($result, OCI_ASSOC + OCI_RETURN_NULLS)) {
echo "<tr>";
foreach ($row as $field_value) {
echo "<td>$field_value</td>";
}
echo "</tr>";
}
echo "</table>";
OCICommit($db_conn);
}
function handleProjectionRequest() {
global $db_conn;
$attribute = $attribute = implode(', ', $_GET['selectAttributes']);
$query = "SELECT DISTINCT $attribute FROM ContentCreators";
$result = executePlainSQL($query);
echo "<h3>Query Result:</h3>";
echo "<table border='1'>";
$field_names = oci_num_fields($result);
echo "<tr>";
for ($i = 1; $i <= $field_names; $i++) {
$field_name = oci_field_name($result, $i);
echo "<th>$field_name</th>";
}
echo "</tr>";
while ($row = oci_fetch_array($result, OCI_ASSOC + OCI_RETURN_NULLS)) {
echo "<tr>";
foreach ($row as $field_value) {
echo "<td>$field_value</td>";
}
echo "</tr>";
}
echo "</table>";
OCICommit($db_conn);
}
function handleJoinRequest() {
global $db_conn;
$contact = $_POST['ccid'];
$result = executePlainSQL(
"SELECT cc.content_creator_contact, cc.content_creator_name, m.itemNumber, m.mType, m.manufacturer
FROM ContentCreators cc, MerchandiseSold1 m
WHERE cc.content_creator_contact = m.content_creator_contact AND cc.content_creator_contact=" . $contact . "");
echo "<table>";
echo "<tr><th>Contact</th><th>ContentCreatorName</th><th>Item#</th><th>Type</th><th>Manufacturer</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><tr>";
}
echo "</table>";
OCICommit($db_conn);
}
function handleAggregationWithGroupByRequest() {
global $db_conn;
$result = executePlainSQL(
"SELECT cc.agency_name, MAX(c.pay_rate)
FROM ContentCreators cc, contracts1 c
WHERE cc.contracts_id = c.contracts_id
GROUP BY cc.agency_name");
echo "<table>";
echo "<tr><th>Agency Name</th><th>Pay</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><tr>";
}
echo "</table>";
OCICommit($db_conn);
}
function handleAggregationWithHaving() {
global $db_conn;
$result = executePlainSQL(
"SELECT m2.manufacturer, MAX(m2.price)
FROM MerchandiseSold2 m2
GROUP BY m2.manufacturer
HAVING MAX(m2.price) > 30"
);
echo "<table>";
echo "<tr><th>Merch Manufacturer</th><th>max merch price</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><tr>";
}
echo "</table>";
OCICommit($db_conn);
}
function handleNestedAggregationWithGroupByRequest() {
global $db_conn;
$result = executePlainSQL(
"SELECT MAX(x.avg)
FROM (SELECT cc.agency_name as agency_name, AVG(y.subscriber_count) as avg
FROM Youtubers y
INNER JOIN ContentCreators cc
ON cc.content_creator_contact = y.content_creator_contact
GROUP BY cc.agency_name) x"
);
echo "<table>";
echo "<tr><th>subscriber count average</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><tr>";
}
echo "</table>";
OCICommit($db_conn);
}
// Find the agencies who can give all types of contracts
function handleDivisionRequest() { // TODO
global $db_conn;
$result = executePlainSQL(
"SELECT a.agency_name
FROM Agencies a
WHERE NOT EXISTS
(
SELECT c.contracts_id
FROM Contracts1 c
MINUS
SELECT o.contracts_id
FROM Offer o
WHERE o.agency_name = a.agency_name
)"
);
echo "<table>";
echo "<tr><th>Agency Name</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td><tr>";
}
echo "</table>";
OCICommit($db_conn);
}
// HANDLE ALL POST ROUTES
// A better coding practice is to have one method that reroutes your requests accordingly. It will make it easier to add/remove functionality.
function handlePOSTRequest() {
if (connectToDB()) {
if (array_key_exists('updateQueryRequest', $_POST)) {
handleUpdateRequest();
} else if (array_key_exists('insertQueryRequest', $_POST)) {
handleInsertRequest();
} else if (array_key_exists('deleteQueryRequest', $_POST)) {
handleDeleteRequest();
} else if (array_key_exists('selectQueryRequest', $_POST)) {
handleSelectRequest();
} else if (array_key_exists('joinQueryRequest', $_POST)) {
handleJoinRequest();
}
disconnectFromDB();
}
}
// HANDLE ALL GET ROUTES
// A better coding practice is to have one method that reroutes your requests accordingly. It will make it easier to add/remove functionality.
function handleGETRequest() {
if (connectToDB()) {
if (array_key_exists('showAllTablesRequest', $_GET)) {
handleShowAllTablesRequest();
} else if (array_key_exists('aggregationWithGroupByQueryRequest', $_GET)) {
handleAggregationWithGroupByRequest();
} else if (array_key_exists('projectionQueryRequest', $_GET)) {
handleProjectionRequest();
} else if (array_key_exists('aggregationWithHavingQueryRequest', $_GET)) {
handleAggregationWithHaving();
} else if (array_key_exists('nestedAggregationWithGroupByQueryRequest', $_GET)) {
handleNestedAggregationWithGroupByRequest();
} else if (array_key_exists('divisionQueryRequest', $_GET)) {
handleDivisionRequest();
}
disconnectFromDB();
}
}
if (isset($_POST['updateSubmit']) || isset($_POST['insertSubmit']) || isset($_POST['deleteSubmit']) || isset($_POST['selectSubmit']) || isset($_POST['joinSubmit'])) {
handlePOSTRequest();
} else if (isset($_GET['showAllTablesRequest']) || isset($_GET['aggregationWithGroupBySubmit']) || isset($_GET['projectionSubmit']) || isset($_GET['aggregationWithHavingSubmit']) || isset($_GET['nestedAggregationWithGroupBySubmit']) || isset($_GET['divisionSubmit'])) {
handleGETRequest();
}
?>
</body>
</html>