What are potential security risks when handling user input in PHP, and how can they be mitigated?

One potential security risk when handling user input in PHP is the possibility of SQL injection attacks, where malicious users can manipulate database queries through input fields. This can be mitigated by using prepared statements and parameterized queries to prevent user input from being directly concatenated into SQL queries.

// Using prepared statements to mitigate SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();