What are the common pitfalls to avoid when working with form data and SQL queries in PHP?

One common pitfall is not properly sanitizing form data before using it in SQL queries, which can lead to SQL injection attacks. To avoid this, always use prepared statements with parameterized queries to securely interact with the database.

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