-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.php
executable file
·335 lines (270 loc) · 10 KB
/
insert.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
<?php
/*
Allows user to insert a new professor information into database
*/
session_start();
// creates the insertion record form
// since this form is used multiple times in this file, made it for easily reuse!
function renderForm($name, $dept, $title, $office, $phone, $email, $photo, $website, $resume, $scholar, $edu, $publications, $areas, $error)
{
?>
<!DOCTYPE HTML>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<head>
<title>Create the Information of the New Professor</title>
<SCRIPT>
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post" class="w3-container w3-card-4 w3-light-grey">
<h2>New professor</h2>
<div class="w3-third">
<p>
<label class="w3-label"><strong>Name: *</strong></label>
<input class="w3-input w3-border" type="text" name="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label class="w3-label"><strong>Department: *</strong></label>
<select class="w3-select w3-border" type="text" name="dept">
<option value="" disabled selected>Choose the department</option>
<option value="Electrical and Computer Engineering">Electrical and Computer Engineering</option>
<option value="Computer Science and Engineering">Computer Science and Engineering</option>
<option value="Material Science and Engineering">Material Science and Engineering</option>
<option value="Aerospace Engineering">Aerospace Engineering</option>
<option value="Biomedical Engineering">Biomedical Engineering</option>
<option value="Chemial Engineering">Chemial Engineering</option>
<option value="Civil Engineering">Civil Engineering</option>
<option value="Engineering Technology and Industrial">Engineering Technology and Industrial</option>
<option value="Industrial and Systems Engineering">Industrial and Systems Engineering</option>
<option value="Nuclear Engineering">Nuclear Engineering</option>
<option value="Ocean Engineering">Ocean Engineering</option>
<option value="Petroleum Engineering">Petroleum Engineering</option>
</select>
<p>
<label class="w3-label">Title:</label>
<input class="w3-input w3-border" type="text" name="title" value="<?php echo $title; ?>"/>
</p>
<p>
<label class="w3-label">Office:</label>
<input class="w3-input w3-border" type="text" name="office" value="<?php echo $office; ?>"/>
</p>
<p>
<label class="w3-label">Phone:</label>
<input class="w3-input w3-border" type="text" name="phone" value="<?php echo $phone; ?>"/>
</p>
<p>
<label class="w3-label"><strong>Email: *</strong></label>
<input class="w3-input w3-border" type="text" name="email" value="<?php echo $email; ?>"/>
</p>
<p>
<label class="w3-label">Photo(hyperlink):</label>
<input class="w3-input w3-border" type="text" name="photo" value="<?php echo $photo; ?>"/>
</p>
<p>
<label class="w3-label">Website:</label>
<input class="w3-input w3-border" type="text" name="website" value="<?php echo $website; ?>"/>
</p>
<p>
<label class="w3-label">Resume(hyperlink):</label>
<input class="w3-input w3-border" type="text" name="resume" value="<?php echo $resume; ?>"/>
</p>
<p>
<label class="w3-label">Google Scholar Page(hyperlink):</label>
<input class="w3-input w3-border" type="text" name="scholar" value="<?php echo $scholar; ?>"/>
</p>
<p>
<label class="w3-label">Education:</label>
<input class="w3-input w3-border" type="text" name="edu" value="<?php echo $edu; ?>"/>
</p>
<p>
<label class="w3-label">Publications:</label>
<table class="w3-table w3-stripedd w3-border" id="pub_table">
<?php
if(isset($_POST['publications'])){
$cnt = count($_POST['publications']);
for($i = 0; $i < $cnt; $i++)
echo '<tr><td><input class="w3-check" type="checkbox" name="chk"/></td>
<td><input class="w3-input w3-border" type="text" name="publications[]" value="'. $_POST['publications'][$i].'"/></td></tr>';
}
else{
echo '<tr><td><input class="w3-check" type="checkbox" name="chk"/></td>
<td><input class="w3-input w3-border" type="text" name="publications[]"/></td></tr>';
}
?>
</table>
<p>
<input class="w3-btn w3-gray" type="button" value="New Publication" onclick="addRow('pub_table')"/>
<input class="w3-btn w3-gray" type="button" value="Delete Publication" onclick="deleteRow('pub_table')"/>
</p>
<p>
<label class="w3-label">Research Areas:</label>
<table class="w3-table w3-stripedd w3-border" id="area_table">
<?php
if(isset($_POST['areas'])){
$cnt = count($_POST['areas']);
for($i = 0; $i < $cnt; $i++)
echo '<tr><td><input class="w3-check" type="checkbox" name="chk"/></td>
<td><input class="w3-input w3-border" type="text" name="areas[]" value="'. $_POST['areas'][$i].'"/></td></tr>';
}
else{
echo '<tr><td><input class="w3-check" type="checkbox" name="chk"/></td>
<td><input class="w3-input w3-border" type="text" name="areas[]"/></td></tr>';
}
?>
</table>
<p>
<input class="w3-btn w3-gray" type="button" value="New Area" onclick="addRow('area_table')"/>
<input class="w3-btn w3-gray" type="button" value="Delete Area" onclick="deleteRow('area_table')"/>
</p>
<p><strong>* Required</strong></p>
<input class="w3-btn w3-green" type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect_db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$dept = mysql_real_escape_string(htmlspecialchars($_POST['dept']));
$title = $_POST['title'];
$office = $_POST['office'];
$phone = $_POST['phone'];
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$photo = $_POST['photo'];
$website= $_POST['website'];
$resume = $_POST['resume'];
$scholar= $_POST['scholar'];
$edu = $_POST['edu'];
$publications = $_POST['publications'];
$areas = $_POST['areas'];
// check that email/dept fields are both filled in
if ($name == '' || $email == '' || $dept == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($name, $dept, $title, $office, $phone, $email, $photo, $website, $resume, $scholar, $edu, $publications, $areas, $error);
}
else
{
// insert the new data to the database
// if the name and department matched, insert the new publication/area
// else if this is a new professor, create the record!
$myquery = "SELECT basic.name, basic.dept FROM basic WHERE basic.name = '$name' AND dept = '$dept'";
$result = mysql_query($myquery)
or die("Cannot process the query : ". $myquery. mysql_error());
$row = mysql_fetch_array($result);
if(!$row){
// insert into basic and academic table for new professor
$q = "INSERT INTO basic (id, name, dept, title, office, phone, email, photo) VALUES (null, '$name', '$dept', '$title', '$office', '$phone', '$email', '$photo')";
mysql_query($q)
or die("Cannot process the query : ". $q . mysql_error());
$q = "INSERT INTO academic (id, name, website, resume, scholar, email, education) VALUES (null, '$name', '$website', '$resume', '$scholar', '$email', '$edu')";
mysql_query($q)
or die("Cannot process the query : ". $q . mysql_error());
// insert into publication and research table
}
// insert into publication and research table
if(isset($_POST['publications']) ){
$cnt=count($_POST['publications']);
for($i = 0; $i < $cnt; $i++){
if(!empty($_POST['publications'][$i])){
$pub = $_POST['publications'][$i];
$q = "INSERT INTO publication (id, author, paper) VALUES (null, '$name', '$pub')";
mysql_query($q)
or die("Cannot process the query : ". $q . mysql_error());
}
}
}
if(isset($_POST['areas']) ){
$cnt=count($_POST['areas']);
for($i = 0; $i < $cnt; $i++){
if(!empty($_POST['areas'][$i])){
$area = $_POST['areas'][$i];
$q = "INSERT INTO research (id, name, area) VALUES (null, '$name', '$area')";
mysql_query($q)
or die("Cannot process the query : ". $q . mysql_error());
}
}
}
// once done, redirect back to the view page
header("Location: users_page.php");
}
}
else
// if the form hasn't been submitted, simply deplay the form
{
// get the data:
$name = $_POST['name'];
$dept = $_POST['dept'];
$title = $_POST['title'];
$office = $_POST['office'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$photo = $_POST['photo'];
$website= $_POST['website'];
$resume = $_POST['resume'];
$scholar= $_POST['scholar'];
$edu = $_POST['edu'];
$publications = $_POST['publications'];
$areas = $_POST['areas'];
renderForm($name, $dept, $title, $office, $phone, $email, $photo, $website, $resume, $scholar, $edu, $publications, $areas, '');
}
?>