I would like to know why my code is vulnerable to SQL Injection, despite me using PDO prepare and execute?
Heres my code:
$conn = new PDO('mysql:host=localhost;dbname=SQLHack', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$id = $_GET['id'];
$query = "SELECT * FROM users WHERE username ='$id'";
$data = $conn->prepare($query) or die("ERROR: " . implode(":", $conn->errorInfo()));
$data->execute(array(':username'));
$data->setFetchMode(PDO::FETCH_BOTH);
while ($r = $data->fetch()) {
echo "<br />\n";
print_r("ID: " . $r['id'] . " Username: " . $r['username']);
}
The line that this is vulnerable to is this one, but if its vulnerable to this it is vulnerable to many other.
' or 1=1 union select 1,2,3'
If that is entered it reveals information when it really shouldn't.