-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
241 lines (217 loc) · 6.93 KB
/
profile.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
<?php
session_start();
$page = "My Profile";
/*
protect route
check if user is not authenticated
to redirect the user to login page
*/
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
}
// db connection
require_once 'config/db.php';
// global vars
$error = false;
$photo = $photo_err = "";
$user_id = $_SESSION['user_id'];
// update profile picture
if (isset($_POST['change_picture'])) {
$_POST = filter_input_array(INPUT_POST,FILTER_SANITIZE_STRING);
$photo = $_FILES['picture']["name"];
$photo_temp = $_FILES['picture']['tmp_name'];
$target_dir = "assets/images/users/";
if (empty($photo)) {
$photo = "user.jpg";
}else{
// check for valid image extension
switch($_FILES['picture']['type']){
case 'image/jpeg':
case 'image/jpg':
$ext = 'jpg';
break;
case 'image/gif':
$ext = 'gif';
break;
case 'image/png':
$ext = 'png';
break;
case 'image/tiff':
$ext = 'tif';
break;
case 'image/webp':
$ext = 'webp';
break;
default:
$ext = '';
break;
}
// validate extension
if(!$ext){
$error = true;
$photo_err = "Sorry, only JPG, JPEG, PNG, webp & GIF files are allowed.";
}
if (!$error) {
// rename image before inserting to database
$photo = "oaccfl_user_".time().".".$ext;
// prepare an sql statement
$sql = "UPDATE users SET picture=:picture WHERE id=:user_id";
$stmt = $pdo->prepare($sql);
// bind param
$stmt->bindParam(':picture', $photo, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
// only upload image if user select a new image
if(!empty($_FILES['picture']['name'])){
move_uploaded_file($photo_temp, $target_dir.$photo);
}
// prepare to execute
if($stmt->execute()){
$_SESSION['success-msg'] = "Picture updated sucessfully";
}else{
$_SESSION['danger-msg'] = "Error changing profile photo";
}
unset($stmt);
unset($pdo);
}
}
}
$name = $name_err = $phone = $photo_err = "";
// update profile info
if (isset($_POST['update_info'])) {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$name = ucwords(test_input($_POST['name']));
$phone = test_input($_POST['phone']);
// validate name
if(empty($name)){
$error = true;
$name_err = "Please enter a name";
}
// validate phone
if(empty($phone)){
$error = true;
$phone_err = "Please enter a phone";
}
// if no error
if (!$error) {
// prepare an sql statement
$sql = "UPDATE users SET name=:name, phone=:phone WHERE id=:user_id";
$stmt = $pdo->prepare($sql);
// bind params
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
// prepare to execute
if($stmt->execute()){
$_SESSION['name'] = $name;
$_SESSION['success-msg'] = "profile info updated sucessfully";
}else{
$_SESSION['danger-msg'] = "error updating profile info";
}
unset($stmt);
}
}
require_once 'includes/head.php';
require_once 'includes/nav.php';
// fetch user data
$sql = "SELECT * FROM users WHERE id=:user_id";
if($stmt = $pdo->prepare($sql)){
// bind param
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
// attempt to excute
if($stmt->execute()){
// store data in a varible
$data = $stmt->fetch();
}
}
?>
<div class="container-fluid">
<div class="row">
<div class="col-md-3 mt-4">
<?php require_once 'includes/sidebar.php'; ?>
</div>
<div class="col-md-9 mt-4">
<h3>My Profile</h3>
<hr>
<?php if (isset($_SESSION['success-msg']) || isset($_SESSION['danger-msg'])): ?>
<?php if ($_SESSION['success-msg']): ?>
<div class="alert alert-success alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Success! </strong><?php echo $_SESSION['success-msg']; ?>
</div>
<?php else: ?>
<div class="alert alert-warning alert-dismissable fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Warning! </strong><?php echo $_SESSION['danger-msg']; ?>
</div>
<?php endif;
unset($_SESSION['success-msg']);
unset($_SESSION['danger-msg']);
endif
?>
<div class="row">
<div class="col-md-4">
<img src="assets/images/users/<?php echo $data['picture']; ?>" alt="<?php echo $data['name']; ?>" class="img-fluid profile-img">
</div>
<div class="col-md-8">
<button type="button" class="btn btn-link text-cyan" id="changePic">Change profile picture</button>
<br>
<button type="button" class="btn btn-link text-cyan" id="editInfo">Edit profile info</button>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" id="changePicForm" class="hide">
<div class="form-group">
<label for="picture">Upload</label>
<input type="file" name="picture" id="picture" class="<?php echo !empty($photo_err) ? 'is-invalid' : '' ?> form-control-file">
<span class="invalid-feedback"><?php echo $photo_err; ?></span>
<input type="submit" value="Upload" name="change_picture" class="btn btn-success">
</div>
</form>
</div>
</div>
<table class="table " id="profileInfo">
<thead>
<tr>
<th>Info</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>Full Name</td>
<td><?php echo $data["name"]; ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $data["email"]; ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo $data["phone"]; ?></td>
</tr>
</tbody>
</table>
<div class="row " style="display: none" class="hide" id="editProfile">
<div class="col-md-6" >
<h5 class="mt-2">Edit Profile Info</h5>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<div class="form-group">
<label for="name">Full Name:</label>
<input type="text" name="name" id="name" class="form-control <?php echo !empty($name_err) ? 'is-invalid' : '' ?>" value="<?php echo $data['name'] ?>">
<span class="invalid-feedback"><?php echo $name_err ?></span>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" name="email" disabled id="email" class="form-control" value="<?php echo $data['email'] ?>">
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<input type="text" name="phone" id="phone" class="form-control <?php echo !empty($photo_err) ? 'is-invalid' : '' ?>" value="<?php echo $data['phone'] ?>">
<span class="invalid-feedback"><?php echo $phone_err; ?></span>
</div>
<button type="submit" name="update_info" class="btn btn-success">Update profile info</button>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- end main content -->
<?php require_once 'includes/foot.php';?>