-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin_page3.php
More file actions
275 lines (222 loc) · 8.39 KB
/
Copy pathAdmin_page3.php
File metadata and controls
275 lines (222 loc) · 8.39 KB
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
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Administrator</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.0.7/css/all.css">
<link href="https://fonts.googleapis.com/css?family=Be+Vietnam:400,600,800&display=swap" rel="stylesheet">
</head>
<body>
<?php
$servname = "localhost";
$conn = new mysqli($servname, "root","", "college_db");
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fname = isset($_POST['fname']) ? $_POST['fname'] : "";
$mname = isset($_POST['mname']) ? $_POST['mname'] : "";
$lname = isset($_POST['lname']) ? $_POST['lname'] : "";
$ugend = isset($_POST['ugend']) ? $_POST['ugend'] : "";
switch ($ugend) {
case 'Male':
$ugend = 'M';
break;
case 'Female':
$ugend = 'F';
break;
case 'Transgender':
$ugend = 'T';
break;
default:
// Handle default case or validation
break;
}
// Use prepared statement to prevent SQL injection
$updatePersonStmt = $conn->prepare("UPDATE PERSON SET FirstName=?, MiddleName=?, LastName=?, Gender=? WHERE PersonID=?");
$updatePersonStmt->bind_param("ssssi", $fname, $mname, $lname, $ugend, $_SESSION["personid"]);
$updatePersonStmt->execute();
$updatePersonStmt->close();
if ($_SESSION["phchanged"] == "true") {
$deletePhoneStmt = $conn->prepare("DELETE FROM PHONE WHERE PersonID=?");
$deletePhoneStmt->bind_param("i", $_SESSION["personid"]);
$deletePhoneStmt->execute();
$deletePhoneStmt->close();
$phones = isset($_POST['phones']) ? $_POST['phones'] : array();
$insertPhoneStmt = $conn->prepare("INSERT INTO PHONE VALUES (?, ?)");
$insertPhoneStmt->bind_param("ii", $_SESSION["personid"], $phone);
foreach ($phones as $phone) {
$insertPhoneStmt->execute();
}
$insertPhoneStmt->close();
$_SESSION["phchanged"] = "false";
}
}
if ($_SESSION["usertype"] == "instructor") {
// Check if "insdept" is set in the $_POST array
if (isset($_POST['insdept'])) {
$insdept = $_POST['insdept'];
// Use prepared statement to prevent SQL injection
$stmt = $conn->prepare("SELECT DeptNo FROM DEPARTMENT WHERE DeptName = ?");
$stmt->bind_param("s", $insdept);
$stmt->execute();
$res = $stmt->get_result();
// Check if the query was successful
if ($res) {
$deptData = $res->fetch_assoc();
// Check if the result contains the "DeptNo" key
if (isset($deptData['DeptNo'])) {
$deptno = $deptData['DeptNo'];
$sql = "UPDATE INSTRUCTOR SET DeptNo = ? WHERE InstructorID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("is", $deptno, $_SESSION["userid"]);
$stmt->execute();
} else {
echo "Department not found.";
}
} else {
echo "Error in the query.";
}
}
}
?>
<div class="page-container">
<div class="content-wrap">
<div class="user-label rect-circ">
<span class="rect-circ">ADMINISTRATOR</span>
</div>
<div class="btn-logout rect-circ" onClick="goBack()">
<span class="rect-circ">BACK</span>
<div class="rect-circ"><-</div>
</div>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" class="Admin_page3-edit">
<input type="submit" value="SUBMIT" class="btn-submit rect-circ"/>
<div class="user-type-label">
<?php
if ($_SESSION["usertype"] == "student")
echo '<span>STUDENT</span>';
elseif ($_SESSION["usertype"] == "instructor")
echo '<span>INSTRUCTOR</span>';
?>
</div>
<?php
$userarray = array();
if ($_SESSION["usertype"] == "student") {
$sql = "SELECT StudentID, FirstName, MiddleName, LastName, Gender, STUDENT.PersonID
FROM STUDENT, PERSON
WHERE STUDENT.PersonID = PERSON.PersonID
AND StudentID = '";
$sql .= $_SESSION["userid"]."'";
$res = $conn->query($sql);
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
$_SESSION["personid"] = $row['PersonID'];
array_push($userarray,
$row['StudentID'], $row['FirstName'], $row['MiddleName'], $row['LastName'], $row['Gender']);
}
}
}
elseif ($_SESSION["usertype"] == "instructor") {
$sql = "SELECT InstructorID, FirstName, MiddleName, LastName, Gender, PERSON.PersonID
FROM INSTRUCTOR, PERSON
WHERE INSTRUCTOR.PersonID = PERSON.PersonID
AND InstructorID = '";
$sql .= $_SESSION["userid"]."'";
$res = $conn->query($sql);
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
$_SESSION["personid"] = $row['PersonID'];
array_push($userarray,
$row['InstructorID'], $row['FirstName'], $row['MiddleName'], $row['LastName'], $row['Gender']);
}
}
}
echo '<div class="user-person">
<div class="rect-round-sm">'.$userarray[0].'</div>
<input type="text" name="fname" value="'.$userarray[1].'" class="edit-id rect-round-sm">
<input type="text" name="mname" value="'.$userarray[2].'" class="edit-id rect-round-sm">
<input type="text" name="lname" value="'.$userarray[3].'" class="edit-id rect-round-sm">
<select name="ugend" class="rect-round-sm">';
if ($userarray[4] == 'M')
echo '<option selected="selected">Male</option>';
else
echo '<option>Male</option>';
if ($userarray[4] == 'F')
echo '<option selected="selected">Female</option>';
else
echo '<option>Female</option>';
if ($userarray[4] == 'T')
echo '<option selected="selected">Transgender</option>';
else
echo '<option>Transgender</option>';
echo '</select>
</div>';
?>
<?php
$sql = "SELECT PhNo FROM PHONE WHERE PersonID = '";
$sql .= $_SESSION["personid"]."'";
$res = $conn->query($sql);
$phones = array(); $pcnt = 0;
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
array_push($phones, $row['PhNo']);
$pcnt++;
if ($pcnt == 3) break;
}
}
$firstPhoneNumber = isset($phones[0]) ? $phones[0] : "";
echo '<div class="label-phone"><span>PHONE:</span></div>';
echo '<input type="number" class="phno rect-round-sm" name="phones[]" value="' . $firstPhoneNumber . '" maxlength="10">';
$_SESSION["phchanged"] = "true";
if ($_SESSION["usertype"] == "instructor") {
echo '<div class="label-dept"><span>DEPARTMENT:</span></div>
<select class="user-dept-sel rect-round-sm" name="insdept">';
$departments = array();
$sql = "SELECT * FROM DEPARTMENT ORDER BY DeptNo";
$res = $conn->query($sql);
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
array_push($departments, array($row['DeptNo'], $row['DeptName']));
}
}
$sql = "SELECT DeptNo FROM INSTRUCTOR WHERE InstructorID = '";
$sql .= $_SESSION["userid"]."'";
$res = $conn->query($sql);
$deptno = 0;
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
$deptno = $row['DeptNo'];
}
}
for ($i = 0; $i < COUNT($departments); $i++) {
if ($deptno == $departments[$i][0])
echo '<option selected="selected" name="insdept" value="'.$departments[$i][1].'">'.$departments[$i][1].'</option>';
else
echo '<option name="insdept" value="'.$departments[$i][1].'">'.$departments[$i][1].'</option>';
}
echo '</select>';
}
?>
</form>
</div>
</div>
<script>
function goBack() {
window.location.href = 'Admin_page1.php';
}
</script>
<script>
function logout() {
document.cookie = "courseid" + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = "loggedin" + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = "logout=yes";
window.location.href = 'Login_page.php';
}
</script>
</body>
</html>