-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditprofile.php
349 lines (325 loc) · 18.5 KB
/
editprofile.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
<?php
@ob_start();
session_start();
date_default_timezone_set('Asia/Singapore');
if (!isset( $_SESSION['NUSEmail'] ) ) {
// Redirect them to the login page
if ($_SERVER['SERVER_PORT'] == '80' || $_SERVER['SERVER_PORT'] =='443'){
header("Location: http://localhost/orbital/signup.php");
}
else{
header("Location: http://localhost:8080/orbital/signup.php");
}
}
?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link rel="icon" href="img\studylah_logo.jpg" type="image/jpg">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css\navbar.css">
<link rel="stylesheet" href="css\editprofile.css">
<title>StudyLah</title>
</head>
<body onload="populateSelectFields();" style="font-family: 'Inter', sans-serif;">
<!--navbar-->
<?php include('header.php');?>
<!--profile section-->
<section style="background-color:#424240;">
<section id="profilesection">
<form method="post" enctype="multipart/form-data">
<div>
<div class="bigtext">Edit Profile</div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname= "orbital";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$useremail=$_SESSION['NUSEmail'];
$sqlgetuser="SELECT * FROM users WHERE NUSEmail='$useremail'";
$resultgetuser=mysqli_query($conn,$sqlgetuser);
if(mysqli_num_rows($resultgetuser) > 0){//valid
while($rowgetuser=mysqli_fetch_array($resultgetuser)){
//display profile picture
$profilepic=$rowgetuser['ProfilePic'];
if($profilepic=="" || $profilepic==NULL){
echo "<div><img src=\"img/user.png\" id=\"profilepic\" alt=\"profilepic\" width=\"250px\" height=\"250px\" onclick=\"clickonFile()\">
<input type=\"file\" id=\"newProfilePicture\" name=\"newProfilePicture\" style=\"display: none;\" onchange=\"loadFile(event)\"/>
<div><img id=\"output\" width=\"250px\" height=\"250px\" style=\"border-radius:50%;\" hidden/></div>
</div>";
//output is the output pic on upload
}
else{
echo "<div><img src=\"userprofilepic/$profilepic\" id=\"profilepic\" alt=\"profilepic\" width=\"250px\" height=\"250px\" onclick=\"clickonFile()\">
<input type=\"file\" id=\"newProfilePicture\" name=\"newProfilePicture\" style=\"display: none;\" onchange=\"loadFile(event)\"/>
<div><img id=\"output\" width=\"250px\" height=\"250px\" style=\"border-radius:50%;\" hidden/></div>
</div>";
}
echo "<input type=\"text\" value=\"$profilepic\" name=\"userLastProfilePic\" hidden style=\"display:none\">"; //last profilepicture
//table first section
//edit name
$fullname= $rowgetuser['FullName'];
echo "<table class=\"centertable\">";
echo "<tr><td>Name:</td>
<td><input type=\"text\" value=\"$fullname\" name=\"editFullName\" required></td>";
echo "</tr>";
//edit course
$course= $rowgetuser['Course'];
echo "<tr><td>Course:</td>
<td><select id=\"editCourse\" name=\"editCourse\" required>
<option value=\"Data Science and Economics\">Data Science and Economics</option>
<option value=\"Food Science and Technology\">Food Science and Technology</option>
<option value=\"Humanities Sciences\">Humanities Sciences</option>
<option value=\"Pharmaceutical Science\">Pharmaceutical Science</option>
<option value=\"Philosophy, Politics, Economics\">Philosophy, Politics, Economics</option>
<option value=\"Architecture\">Architecture</option>
<option value=\"Industrial Design\">Industrial Design</option>
<option value=\"Landscape Architecture\">Landscape Architecture</option>
<option value=\"Project & Facilities\">Project & Facilities</option>
<option value=\"Real Estate\">Real Estate</option>
<option value=\"Biomedical Engineering\">Biomedical Engineering</option>
<option value=\"Civil Engineering\">Civil Engineering</option>
<option value=\"Chemical Engineering\">Chemical Engineering</option>
<option value=\"Engineering Science\">Engineering Science</option>
<option value=\"Environmental Engineering\">Environmental Engineering</option>
<option value=\"Electrical Engineering\">Electrical Engineering</option>
<option value=\"Mechanical Engineering\">Mechanical Engineering</option>
<option value=\"Industrial and Systems Engineering\">Industrial and Systems Engineering</option>
<option value=\"Material Science Engineering\">Material Science Engineering</option>
<option value=\"Business Administration (Accountancy)\">Business Administration (Accountancy)</option>
<option value=\"Information Security\">Information Security</option>
<option value=\"Computer Science\">Computer Science</option>
<option value=\"Information System\">Information System</option>
<option value=\"Business Analytics\">Business Analytics</option>
<option value=\"Computer Engineering\">Computer Engineering</option>
<option value=\"Dentistry\">Dentistry</option>
<option value=\"Undergraduate Law Programme\">Undergraduate Law Programme</option>
<option value=\"Medicine\">Medicine</option>
<option value=\"Nursing\">Nursing</option>
<option value=\"Pharmacy\">Pharmacy</option>
<option value=\"Music\">Music</option>
</select></td>";
echo "<input id=\"HidCourse\" type=\"text\" value=\"$course\" hidden>";
echo "</tr>";
//edit year of study
$yearofstudy = $rowgetuser['YearOfStudy'];
echo "<tr><td>Year of Study:</td>
<td><select id=\"editYearOfStudy\" name=\"editYearOfStudy\" required>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select></td>";
echo "<input id=\"HidYearOfStudy\" type=\"number\" value=\"$yearofstudy\" hidden>";
echo "</tr>";
//edit bio
$bio = $rowgetuser['Bio'];
echo "<tr><td>Bio:</td>
<td><textarea cols=\"30\" rows=\"10\" name=\"editBio\" maxlength=\"200\">$bio</textarea></td>";
echo "</tr>";
//edit residency stat
$residencystat= $rowgetuser['ResidencyStatus'];
echo "<tr><td>Residency:</td>
<td><select id=\"editResidency\" name=\"editResidency\" required>
<option value=\"No\">Does not stay in campus</option>
<option value=\"Yes\">Stays in campus</option>
</select></td>";
echo "<input id=\"HidResidency\" type=\"text\" value=\"$residencystat\" hidden>";
echo "</tr>";
$currentmod= $rowgetuser['CurrentMod'];
echo "<tr><td>Current Mods:</td>
<td><textarea cols=\"30\" rows=\"10\" name=\"editCurrentMod\" maxlength=\"300\">$currentmod</textarea></td>";
echo "</tr>";
$pastmod= $rowgetuser['PastMod'];
echo "<tr><td>Past Mods:</td>
<td><textarea cols=\"30\" rows=\"10\" name=\"editPastMod\" maxlength=\"300\">$pastmod</textarea></td>";
echo "</tr>";
$cca= $rowgetuser['CCA'];
echo "<tr><td>CCAs/Clubs:</td>
<td><textarea cols=\"30\" rows=\"10\" name=\"editCCA\" maxlength=\"300\">$cca</textarea></td>";
echo "</tr>";
echo "</table>";
}
}
else{
if ($_SERVER['SERVER_PORT'] == '80' || $_SERVER['SERVER_PORT'] =='443'){
$url="http://localhost/orbital/signup.php";
header('Location:' . $url);
}
else{
$url="http://localhost:8080/orbital/signup.php";
header('Location:' . $url);
}
}
?>
</div>
<div>
<button type="submit" id="confirmbutton" name="confirmbutton">Confirm changes</button>
</div>
</form>
</section>
</section>
<?php
$nusemail= $_SESSION['NUSEmail'];
if (isset($_POST['confirmbutton'])){
$imagetoken = bin2hex(random_bytes(15));//Generate unique random token to append to imagename
if(!$_FILES['newProfilePicture']['name']==""){ //something is uploaded
$filebasename= $imagetoken.basename($_FILES["newProfilePicture"]["name"]);
$target_dir = $_SERVER['DOCUMENT_ROOT'].'/orbital/userprofilepic';//$_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/dt-the7/trainerprofilepicture';
$target_file = $target_dir . '/' . $filebasename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["newProfilePicture"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["newProfilePicture"]["size"] > 50000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Sorry, only JPG, JPEG & PNG files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
exit();
// if everything is ok, try to upload file
}
else {
if (move_uploaded_file($_FILES["newProfilePicture"]["tmp_name"], $target_file)) {
echo "The file ". $filebasename. " has been uploaded.";
}
else {
echo "Sorry, there was an error uploading your file.";
}
}
$imagename=$filebasename;
//end of image upload
$sqlupdatepic="UPDATE users SET ProfilePic='$imagename' WHERE NUSEmail='$nusemail'";
$resultupdatepic=mysqli_query($conn,$sqlupdatepic);
$thelastfilename=$_POST['userLastProfilePic'];
if($thelastfilename!="" || $thelastfilename!=NULL){ //just upload a new one, if the last file name have, then unlink that file
unlink( $_SERVER['DOCUMENT_ROOT']."/orbital/userprofilepic/$thelastfilename");
}
}
if(!empty($_POST['editFullName'])){
$newfullname = mysqli_real_escape_string($conn, $_POST['editFullName']);//sanitize
$newfullname = filter_var($newfullname, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET FullName='$newfullname' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editCourse'])){
$newcourse = mysqli_real_escape_string($conn,$_POST['editCourse']);//sanitize
$newcourse = filter_var($newcourse, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET Course='$newcourse' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editYearOfStudy'])){
$newyearofstudy = mysqli_real_escape_string($conn,$_POST['editYearOfStudy']);//sanitize
$newyearofstudy = filter_var($newyearofstudy, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET YearOfStudy='$newyearofstudy' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editBio'])){
$newBio = mysqli_real_escape_string($conn, $_POST['editBio']);//sanitize
$newBio = filter_var($newBio, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET Bio='$newBio' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editResidency'])){
$newResidency = mysqli_real_escape_string($conn,$_POST['editResidency']);//sanitize
$newResidency = filter_var($newResidency, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET ResidencyStatus='$newResidency' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editCurrentMod'])){
$newCurrentMod = mysqli_real_escape_string($conn,$_POST['editCurrentMod']);//sanitize
$newCurrentMod = filter_var($newCurrentMod, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET CurrentMod='$newCurrentMod' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editPastMod'])){
$newPastMod = mysqli_real_escape_string($conn,$_POST['editPastMod']);//sanitize
$newPastMod = filter_var($newPastMod, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET PastMod='$newPastMod' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
if(!empty($_POST['editCCA'])){
$newCCA = mysqli_real_escape_string($conn,$_POST['editCCA']);//sanitize
$newCCA = filter_var($newCCA, FILTER_SANITIZE_STRING);
$sqlupdateuser="UPDATE users SET CCA='$newCCA' WHERE NUSEmail='$nusemail'";
$resultupdateuser=mysqli_query($conn,$sqlupdateuser);
}
// Redirect them to the profile page
if ($_SERVER['SERVER_PORT'] == '80' || $_SERVER['SERVER_PORT'] =='443'){
header("Location: http://localhost/orbital/profile.php");
}
else{
header("Location: http://localhost:8080/orbital/profile.php");
}
}
?>
<script>
//profile picture uploads
function clickonFile(){
document.getElementById("newProfilePicture").click();
}
var loadFile = function(event) {
document.getElementById("output").removeAttribute("hidden");
var image = document.getElementById('output');
image.src = URL.createObjectURL(event.target.files[0]);
document.getElementById("profilepic").setAttribute("hidden","hidden");
};
//populating select fields
actualcourse=document.getElementById("HidCourse").value;
actualyearofstudy=document.getElementById("HidYearOfStudy").value;
actualresidency=document.getElementById("HidResidency").value;
function populateSelectFields(){
//course
getcourse=document.getElementById("editCourse");
for(c=0; c < getcourse.length; c++){
theoptionvalues1=getcourse.options[c].value; //the value in the select dropdown
if(actualcourse==theoptionvalues1){
document.getElementById("editCourse").value=actualcourse;
}
}
//yearofstudy
getyearofstudy=document.getElementById("editYearOfStudy");
for(y=0; y < getyearofstudy.length; y++){
theoptionvalues2=getyearofstudy.options[y].value; //the value in the select dropdown
if(actualyearofstudy==theoptionvalues2){
document.getElementById("editYearOfStudy").value=actualyearofstudy;
}
}
//residency
getresidency=document.getElementById("editResidency");
for(r=0; r < getresidency.length; r++){
theoptionvalues3=getresidency.options[r].value; //the value in the select dropdown
if(actualresidency==theoptionvalues3){
document.getElementById("editResidency").value=actualresidency;
}
}
}
</script>
</body>
</html>