What are common security vulnerabilities in PHP code that can lead to PHP injection attacks?

One common security vulnerability in PHP code that can lead to PHP injection attacks is not properly sanitizing user input before using it in SQL queries. This can allow malicious users to inject SQL code into the query, potentially leading to unauthorized access to the database. To prevent this, always use prepared statements with parameterized queries to securely pass user input to the database.

// Using prepared statements to prevent SQL injection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();

$result = $stmt->fetch();