-
Notifications
You must be signed in to change notification settings - Fork 1
/
userprofile.php
261 lines (228 loc) · 8.62 KB
/
userprofile.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
<?php
include 'inc/header.php';
if (empty($_SESSION['email']) && empty($_SESSION['user_id']))
{
header('Location:admin/index.php');
}
?>
<section class="profile">
<div class="container">
<div class="row">
<div class="col-md-9 mx-auto">
<!-- Display logged in users information -->
<?php
$u_id = $_SESSION['user_id'];
$query = "SELECT * FROM users WHERE user_id='$u_id'";
$selectUser = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($selectUser))
{
$user_id = $row['user_id'];
$image = $row['image'];
$fullname = $row['fullname'];
$username = $row['username'];
$email = $row['email'];
$phone = $row['phone'];
$address = $row['address'];
$status = $row['status'];
$user_role = $row['user_role'];
$join_date = $row['join_date']; ?>
<div class="card">
<div class="card-header">
<h3 class="card-title">Update Your Profile</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form class="form-horizontal" method="POST" enctype="multipart/form-data">
<div class="card-body">
<?php
if (isset($msg))
{
echo $msg;
}
?>
<div class="form-group row">
<label for="image" class="col-md-2 col-form-label">Change Picture</label>
<div class="col-md-10">
<?php
if (!empty($image))
{ ?>
<img class="profile-user-img img-fluid img-round" src="admin/dist/img/users/<?php echo $image ?>"
alt="<?php echo $image ?>" alt="User profile picture">
<?php
}
else
{ ?>
<img class="profile-user-img img-fluid img-round" src="admin/dist/img/avatar4.png">
<?php
}
?>
<br><br>
<input type="file" name="image" id="" class="form-control-file">
</div>
</div>
<div class="form-group row">
<label for="fullname" class="col-md-2 col-form-label">Full Name</label>
<div class="col-md-10">
<input type="text" name="fullname" class="form-control" id="fullname" value="<?php echo $fullname; ?>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-2 col-form-label">Email</label>
<div class="col-md-10">
<input type="email" name="email" class="form-control" id="email" value="<?php echo $email; ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-2 col-form-label">Phone</label>
<div class="col-md-10">
<input type="text" name="phone" class="form-control" id="phone" value="<?php echo $phone; ?>">
</div>
</div>
<div class="form-group row">
<label for="address" class="col-md-2 col-form-label">Address</label>
<div class="col-md-10">
<input type="address" name="address" class="form-control" id="address" value="<?php echo $address; ?>">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-2 col-form-label">Password</label>
<div class="col-md-10">
<input type="password" name="password" class="form-control" id="password" placeholder="*****">
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-md-2 col-form-label">Re-Password</label>
<div class="col-md-10">
<input type="repassword" name="repassword" class="form-control" id="repassword" placeholder="*****">
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer text-right">
<input type="hidden" name="updateUserID" value="<?php echo $u_id; ?>">
<input type="submit" name="update" id="" class="btn btn-primary" value="Update Profile">
</div>
<!-- /.card-footer -->
</form>
<!-- Update User Profile Code -->
<?php
if (isset($_POST['update']))
{
$updateUserID = $_POST['updateUserID'];
$fullname = $_POST['fullname'];
$phone = $_POST['phone'];
$address = $_POST['address'];
// Get the Image File and Name
$image = $_FILES['image']['name'];
// Get the Image File and Temporary Folder Name
$image_tmp = $_FILES['image']['tmp_name'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
if (!empty($password) && !empty($image))
{
if ($password == $repassword)
{
// hashed password
$hashedpass = sha1($password);
// Remove old image from the Folder
$removeQuery = "SELECT * FROM users where user_id='$updateUserID'";
$removeImage = mysqli_query($dbc, $removeQuery);
while ($row = mysqli_fetch_assoc($removeImage))
{
$rImage = $row['image'];
unlink("admin/dist/img/users/" . $rImage);
}
// Change Image Name
$randomNumber = rand(0, 9999999);
$imageFileName = $randomNumber . $image;
// Move uploaded file from temporary folder to destination Folder
move_uploaded_file($image_tmp, "admin/dist/img/users/" . $imageFileName);
// Update users data into the database
$query = "UPDATE users SET image='$imageFileName', fullname='$fullname',phone='$phone', address='$address', password='$hashedpass' where user_id='$updateUserID'";
$updateUser = mysqli_query($dbc, $query);
if ($updateUser)
{
$msg = "<div class='alert alert-success'>Your profile updated successfully!</div>";
header("Location:userprofile.php");
}
else
{
die("MySQL Database Error." . mysqli_error($dbc));
}
}
}
else if (!empty($password) && empty($image))
{
if ($password == $repassword)
{
// hashed password
$hashedpass = sha1($password);
// Update users data into the database
$query = "UPDATE users SET fullname='$fullname', phone='$phone', address='$address', password='$hashedpass' where user_id='$updateUserID'";
$updateUser = mysqli_query($dbc, $query);
if ($updateUser)
{
$msg = "<div class='alert alert-success'>Your profile updated successfully!</div>";
header("Location:userprofile.php");
}
else
{
die("MySQL Database Error." . mysqli_error($dbc));
}
}
}
else if (empty($password) && !empty($image))
{
// Remove old image from the Folder
$removeQuery = "SELECT * FROM users where user_id='$updateUserID'";
$removeImage = mysqli_query($dbc, $removeQuery);
while ($row = mysqli_fetch_assoc($removeImage))
{
$rImage = $row['image'];
unlink("admin/dist/img/users/" . $rImage);
}
// Change Image Name
$randomNumber = rand(0, 9999999);
$imageFileName = $randomNumber . $image;
// Move uploaded file from temporary folder to destination Folder
move_uploaded_file($image_tmp, "admin/dist/img/users/" . $imageFileName);
// Update users data into the database
$query = "UPDATE users SET image='$imageFileName', fullname='$fullname', phone='$phone', address='$address' where user_id='$updateUserID'";
$updateUser = mysqli_query($dbc, $query);
if ($updateUser)
{
$msg = "<div class='alert alert-success'>Your profile updated successfully!</div>";
header("Location:userprofile.php");
}
else
{
die("MySQL Database Error." . mysqli_error($dbc));
}
}
else if (empty($password) && empty($image))
{
$query = "UPDATE users SET fullname='$fullname', phone='$phone', address='$address' where user_id='$updateUserID'";
$updateUser = mysqli_query($dbc, $query);
if ($updateUser)
{
$msg = "<div class='alert alert-success'>Your profile updated successfully!</div>";
header("Location:userprofile.php");
}
else
{
die("MySQL Database Error." . mysqli_error($dbc));
}
}
}
?>
</div>
<?php
}
?>
</div>
</div>
</div>
</section>
<?php
include 'inc/footer.php';
?>