What are some best practices for handling user input, such as email addresses, in PHP scripts to prevent SQL injection attacks?

When handling user input, such as email addresses, in PHP scripts, it is important to sanitize and validate the input to prevent SQL injection attacks. One way to do this is by using prepared statements with parameterized queries to securely interact with the database. Additionally, using filter_var() function with FILTER_VALIDATE_EMAIL flag can help validate email addresses.

// Sanitize and validate user input email address
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);

// Prepare a SQL statement using a prepared statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->bindParam(':email', $email);
$stmt->execute();