forked from hariharanrr01/H-COURSE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit.php
93 lines (76 loc) · 2.76 KB
/
edit.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
<?php
session_start();
if (isset($_SESSION['id']) && isset($_SESSION['fname'])) {
if(isset($_POST['fname']) &&
isset($_POST['uname'])){
include "db_conn.php";
$fname = $_POST['fname'];
$uname = $_POST['uname'];
$old_pp = $_POST['old_pp'];
$id = $_SESSION['id'];
if (empty($fname)) {
$em = "Full name is required";
header("Location: edit_profile.php?error=$em");
exit;
}else if(empty($uname)){
$em = "User name is required";
header("Location: edit_profile.php?error=$em");
exit;
}else {
if (isset($_FILES['pp']['name']) AND !empty($_FILES['pp']['name'])) {
$img_name = $_FILES['pp']['name'];
$tmp_name = $_FILES['pp']['tmp_name'];
$error = $_FILES['pp']['error'];
if($error === 0){
$img_ex = pathinfo($img_name, PATHINFO_EXTENSION);
$img_ex_to_lc = strtolower($img_ex);
$allowed_exs = array('jpg', 'jpeg', 'png');
if(in_array($img_ex_to_lc, $allowed_exs)){
$new_img_name = uniqid($uname, true).'.'.$img_ex_to_lc;
$img_upload_path = '../upload/'.$new_img_name;
// Delete old profile pic
$old_pp_des = "../upload/$old_pp";
if(unlink($old_pp_des)){
// just deleted
move_uploaded_file($tmp_name, $img_upload_path);
}else {
// error or already deleted
move_uploaded_file($tmp_name, $img_upload_path);
}
// update the Database
$sql = "UPDATE users
SET fname=?, username=?, pp=?
WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->execute([$fname, $uname, $new_img_name, $id]);
$_SESSION['fname'] = $fname;
header("Location: edit_profile.php?success=Your account has been updated successfully");
exit;
}else {
$em = "You can't upload files of this type";
header("Location: edit_profile.php?error=$em&$data");
exit;
}
}else {
$em = "unknown error occurred!";
header("Location: edit_profile.php?error=$em&$data");
exit;
}
}else {
$sql = "UPDATE users
SET fname=?, username=?
WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->execute([$fname, $uname, $id]);
header("Location: edit_profile.php?success=Your account has been updated successfully");
exit;
}
}
}else {
header("Location: edit_profile.php?error=error");
exit;
}
}else {
header("Location: login.php");
exit;
}