How can one securely store user data in a database when creating a registration system in PHP?

To securely store user data in a database when creating a registration system in PHP, it is important to hash sensitive information such as passwords before storing them. This helps protect user data in case of a data breach. One common method is to use the password_hash() function to securely hash passwords before inserting them into the database.

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

// Store the hashed password in the database
$query = "INSERT INTO users (username, password) VALUES (:username, :password)";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':username' => $_POST['username'], ':password' => $hashed_password));