-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_profile_update.php
142 lines (114 loc) · 4.61 KB
/
user_profile_update.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
<?php
include 'config.php';
session_start();
$user_id = $_SESSION['user_id'];
if(!isset($user_id)){
header('location:login.php');
};
if(isset($_POST['update'])){
$name = $_POST['name'];
$name = filter_var($name, FILTER_SANITIZE_STRING);
$email = $_POST['email'];
$email = filter_var($email, FILTER_SANITIZE_STRING);
$update_profile = $conn->prepare("UPDATE `users` SET name = ?, email = ? WHERE id = ?");
$update_profile->execute([$name, $email, $user_id]);
$old_image = $_POST['old_image'];
$image = $_FILES['image']['name'];
$image_tmp_name = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_folder = 'uploaded_img/'.$image;
if(!empty($image)){
if($image_size > 2000000){
$message[] = 'image size is too large';
}else{
$update_image = $conn->prepare("UPDATE `users` SET image = ? WHERE id = ?");
$update_image->execute([$image, $user_id]);
if($update_image){
move_uploaded_file($image_tmp_name, $image_folder);
unlink('uploaded_img/'.$old_image);
$message[] = 'image has been updated!';
}
}
}
$old_pass = $_POST['old_pass'];
$previous_pass = md5($_POST['previous_pass']);
$previous_pass = filter_var($previous_pass, FILTER_SANITIZE_STRING);
$new_pass = md5($_POST['new_pass']);
$new_pass = filter_var($new_pass, FILTER_SANITIZE_STRING);
$confirm_pass = md5($_POST['confirm_pass']);
$confirm_pass = filter_var($confirm_pass, FILTER_SANITIZE_STRING);
if(!empty($previous_pass) || !empty($new_pass) || !empty($confirm_pass)){
if($previous_pass != $old_pass){
$message[] = 'old password not matched!';
}elseif($new_pass != $confirm_pass){
$message[] = 'confirm password not matched!';
}else{
$update_password = $conn->prepare("UPDATE `users` SET password = ? WHERE id = ?");
$update_password->execute([$confirm_pass, $user_id]);
$message[] = 'password has been updated!';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>user profile update</title>
<!-- font awesome cdn link -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
<!-- custom css file link -->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php
if(isset($message)){
foreach($message as $message){
echo '
<div class="message">
<span>'.$message.'</span>
<i class="fas fa-times" onclick="this.parentElement.remove();"></i>
</div>
';
}
}
?>
<h1 class="title"> update <span>user</span> profile </h1>
<section class="update-profile-container">
<?php
$select_profile = $conn->prepare("SELECT * FROM `users` WHERE id = ?");
$select_profile->execute([$user_id]);
$fetch_profile = $select_profile->fetch(PDO::FETCH_ASSOC);
?>
<form action="" method="post" enctype="multipart/form-data">
<img src="uploaded_img/<?= $fetch_profile['image']; ?>" alt="">
<div class="flex">
<div class="inputBox">
<span>username : </span>
<input type="text" name="name" required class="box" placeholder="enter your name" value="<?= $fetch_profile['name']; ?>">
<span>email : </span>
<input type="email" name="email" required class="box" placeholder="enter your email" value="<?= $fetch_profile['email']; ?>">
<span>profile pic : </span>
<input type="hidden" name="old_image" value="<?= $fetch_profile['image']; ?>">
<input type="file" name="image" class="box" accept="image/jpg, image/jpeg, image/png">
</div>
<div class="inputBox">
<input type="hidden" name="old_pass" value="<?= $fetch_profile['password']; ?>">
<span>old password :</span>
<input type="password" class="box" name="previous_pass" placeholder="enter previous password" >
<span>new password :</span>
<input type="password" class="box" name="new_pass" placeholder="enter new password" >
<span>confirm password :</span>
<input type="password" class="box" name="confirm_pass" placeholder="confirm new password" >
</div>
</div>
<div class="flex-btn">
<input type="submit" value="update profile" name="update" class="btn">
<a href="user_page.php" class="option-btn">go back</a>
</div>
</form>
</section>
</body>
</html>