So I’m creating this test Website and I’m trying to make an edit profile page by user and I want to make the username unchangeble and the user is only able to edit thier profile if they type in thier username in editprofileHTML.php I have this input:
<input type="text" class="form-control" id="username" name="username" value="<?php echo $username; ?>">
I tried to make it disabled but it didn’t work.
editprofile.inc.php:
$updatedData = [
"firstname" => $_POST["firstname"],
"lastname" => $_POST["lastname"],
"phoneNr" => $_POST["phoneNr"],
"username" => $_POST["username"],
"gender" => $_POST["gender"],
];
if (matchingUsername($updatedData["username"]) === true) {
$error = '<p class="error" > Please write your Username correctly </p>';
}
updateUser($updatedData);
$_SESSION["loggedInUser"] = loadUserByUsername($_POST["username"]);
header('Location: ../profile.php');
}
And I also have these functions:
function updateUser($updatedData) {
$dataToUpdate = [];
foreach ($updatedData as $key => $value) {
if ($updatedData[$key] !== "" ) {
$dataToUpdate[$key] = $value;
}
}
if (!empty($dataToUpdate)) {
$sql = 'UPDATE login SET ';
$arrayLength = count($dataToUpdate);
$i = 0;
foreach ($dataToUpdate as $key => $value) {
$sql .= $key . ' = "'.$value.'"';
if (++$i !== $arrayLength) {
$sql .= ', ';
}
}
$sql .= ' WHERE username = "'.$updatedData["username"].'"';
global $conn;
$conn->query($sql);
if ($conn->error){
throw new Exception("Error updating user: " . $conn->error);
}
}
function matchingUsername($username) {
$user = loadUserByUsername($username);
if ($username === $user) {
return true;
} return false;
}
function invalidUsername($username) {
$user = loadUserByUsername($username);
if (!empty($user)) {
return true;
}
}
function loadUserByUsername($username) {
global $conn;
$sql = 'SELECT * FROM login WHERE username = "'.$username.'"';
$result = $conn->query($sql);
return $result->fetch_assoc();
}