What are common pitfalls to avoid when allowing users to change their passwords in PHP?

One common pitfall to avoid when allowing users to change their passwords in PHP is storing passwords in plain text. Instead, passwords should be securely hashed before being stored in the database. Another pitfall is not validating the strength of the new password, which can lead to weak passwords being set. Additionally, it's important to properly sanitize user input to prevent SQL injection attacks.

// Hashing the new password before storing it in the database
$newPassword = password_hash($_POST['new_password'], PASSWORD_DEFAULT);

// Validating the strength of the new password
if(strlen($_POST['new_password']) < 8) {
    // Password is too weak, display an error message
    echo "Password must be at least 8 characters long.";
} else {
    // Update the user's password in the database
    $query = "UPDATE users SET password = '$newPassword' WHERE id = $userId";
    // Execute the query
}