I have a couple of basic questions on parametrized queries
Consider this code:
$id = (int)$_GET['id'];
mysql_query("UPDATE table SET field=1 WHERE id=".$id);
Now the same thing using a parametrized query
$sql = "UPDATE table SET field=1 WHERE id=?";
$q = $db->prepare($sql);
$q->execute(array($_GET['id']));
My questions are:
- is there any situation where the first code (i.e. with the
(int)
cast) is unsafe? - is the second piece of code OK or should I also cast
$_GET['id']
to int? - is there any known vulnerability of the second piece of code? That is, is there any way an SQL attack can be made if I am using the second query?