What potential security risks are present in the PHP code snippet provided for updating passwords?
The potential security risk in the provided PHP code snippet for updating passwords is that it is vulnerable to SQL injection attacks. This is because the password input from the user is directly concatenated into the SQL query, making it possible for attackers to manipulate the query. To solve this issue, you should use prepared statements with parameterized queries to securely handle user input.
// Fix for updating passwords securely using prepared statements
$stmt = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
$stmt->bind_param("si", $new_password, $user_id);
$new_password = password_hash($_POST['new_password'], PASSWORD_DEFAULT);
$user_id = $_SESSION['user_id'];
$stmt->execute();
$stmt->close();