What best practices should be followed when updating passwords in a database using PHP?

When updating passwords in a database using PHP, it is important to follow best practices to ensure security. This includes hashing the password before storing it in the database to protect it from unauthorized access. Additionally, using prepared statements can help prevent SQL injection attacks.

// Assume $newPassword contains the new password input by the user
$newPassword = $_POST['new_password'];

// Hash the new password before storing it in the database
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);

// Update the password in the database using prepared statements
$stmt = $pdo->prepare("UPDATE users SET password = :password WHERE id = :id");
$stmt->execute(['password' => $hashedPassword, 'id' => $userId]);