What are some potential pitfalls when handling user input in PHP forms?

One potential pitfall when handling user input in PHP forms is not properly sanitizing and validating the data, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To mitigate this risk, always sanitize and validate user input before using it in your application.

// Example of sanitizing and validating user input in PHP form
$name = isset($_POST['name']) ? htmlspecialchars(trim($_POST['name'])) : '';
$email = isset($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL) : '';

// Using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('INSERT INTO users (name, email) VALUES (:name, :email)');
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->execute();