What are the potential security risks associated with storing passwords in plain text in a database, and how can they be mitigated in PHP applications?

Storing passwords in plain text in a database is a significant security risk as it exposes sensitive information to anyone with access to the database. To mitigate this risk, passwords should be hashed before storing them in the database. Hashing passwords ensures that even if the database is compromised, the actual passwords cannot be easily retrieved.

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

// Storing the hashed password in the database
$query = "INSERT INTO users (username, password) VALUES ('$username', '$hashed_password')";
// Execute query