What are the potential risks of directly inserting user input into SQL queries in PHP?

Directly inserting user input into SQL queries in PHP can lead to SQL injection attacks, where malicious users can manipulate the SQL query to access or modify the database. To prevent this, you should always use prepared statements with parameterized queries to sanitize and validate user input before executing the SQL query.

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