-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate.php
31 lines (28 loc) · 1.11 KB
/
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
<?php
function updateUser($conn, $id, $u_name, $u_email)
{
$id = trim(mysqli_real_escape_string($conn, $id));
$u_name = trim(mysqli_real_escape_string($conn, htmlspecialchars($u_name)));
$u_email = trim(mysqli_real_escape_string($conn, htmlspecialchars($u_email)));
// IF NAME OR EMAIL IS EMPTY
if (empty($u_name) || empty($u_email)) {
return 'Please fill all required fields.';
}
//IF EMAIL IS NOT VALID
elseif (!filter_var($u_email, FILTER_VALIDATE_EMAIL)) {
return 'Invalid email address.';
} else {
$check_email = mysqli_query($conn, "SELECT `email` FROM `users` WHERE `email` = '$u_email' AND `id`!='$id'");
// IF THE EMAIL IS ALREADY IN USE
if (mysqli_num_rows($check_email) > 0) {
return 'This email is already registered. Please try another.';
}
// UPDATE USER DATA
$query = mysqli_query($conn, "UPDATE `users` SET `name`='$u_name', `email`='$u_email' WHERE `id`='$id'");
// IF USER UPDATED
if ($query) {
return true;
}
return 'Opps something is going wrong!';
}
}