I made a form where users can update username and password, but what if they don’t wanna update the password and leave it blank. I made this code, but I just wanna know about better ways to do it.
if (!empty($_POST['password'])) {
$password = hash("sha256", $_POST['password']);
}
else {
$dbQuery = "SELECT password FROM users WHERE users.id = ".$_GET["update"];
$data = getContent($dbQuery);
foreach($data as $row) {
$password = $row['password'];
}
}
<off-topic>
A $_GET directly in a query! </off-topic>
You could just have a different UPDATE query that only updates the username, not the password. That would save making the extra call to the database to get the password out, just to put it back in the same.
Just one minor suggestion, consider refactoring your code and move the update password functionality to it’s own little form and controller. Update password is different than updating most of the rest of the user profile information. Dealing with it in isolation might make your code a bit easier to maintain.