-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateusername.php
37 lines (32 loc) · 1006 Bytes
/
updateusername.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
<?php
//start session
session_start();
//connect to database
include 'connection.php';
//Get user id from the database
$id = $_SESSION['user_id'];
//Define the error message
$errors = "";
$missingUsername = "<p><strong>Please enter a username!</strong></p>";
//Get the username from the ajax file
if(empty($_POST["username"])){
$errors .= $missingUsername;
}
else{
//to check if the name contains other than white spaces or letters
$username = filter_var($_POST["username"],FILTER_SANITIZE_STRING);
}
if($errors){
$resultmessage = '<div class="alert alert-danger">'. $errors .'</div>';
echo $resultmessage;
exit();
}
//Prepare the variable for sql.
$username = mysqli_real_escape_string($conn,$username);
//Run Query to update Database
$sql = "UPDATE myguests SET username='$username' WHERE user_id='$id'";
$result = mysqli_query($conn,$sql);
if(!$result){
echo "<div class='alert alert-danger'>There was an error storing the new username in the database</div>";
}
?>