The Anatomy of SQL Injection
SQL Injection occurs when untrusted user input is directly concatenated into a dynamic SQL query string, allowing an attacker to manipulate the query structure.
// VULNERABLE CODE - NEVER DO THIS:
$query = "SELECT * FROM users WHERE email = '" . $_POST['email'] . "'";
// SECURE CODE - PARAMETERIZED PDO:
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute([':email' => $_POST['email']]);
With prepared statements, the database engine compiles the query skeleton first. The parameters are transmitted separately as pure data, making it impossible for user input to alter the execution logic.