I am using PDO prepared statements to insert data to database from external xml source, because I do not trust the source 100% I used bindValue on all variables including strings and integers, for example:
SQL:
INSERT INTO table (id, int1, int2, string1, string2)
VALUES (:id, :int1, :int2, :string1, :string2)
In my PDO function:
$sth->bindValue(":id", $id, PDO::PARAM_INT);
$sth->bindValue(":int1", $int1, PDO::PARAM_INT);
$sth->bindValue(":int2", $int2, PDO::PARAM_INT);
$sth->bindValue(":string1", $string1, PDO::PARAM_STR);
$sth->bindValue(":string2", $string2, PDO::PARAM_STR);
Now my question is, if I previousley used int casting to get the integer values, do I still need to use prepared statment for integer values:
$id= (int) $xml->node->attributes()->id
$id will always be an integer, even if the id in the xml file is not an integer the returned value when using (int) will be 0.
Is it safe in this case to just do:
INSERT INTO table (id, int1, int2, string1, string2)
VALUES ($id, $int1, $int2, :string1, :string2)
EDIT (Example of shorter code):
Binding all parameters:
$sql="INSERT INTO table (id, int1, int2, string1, string2)
VALUES (:id, :int1, :int2, :string1, :string2)";
$pars = array(":id"=>$id,":int1"=>$int1,":int2"=>$int2,":string1"=>$string1,
":string2"=>$string2);
$model->insert($sql,$pars);
Without Intergers Binding:
$sql="INSERT INTO table (id, int1, int2, string1, string2)
VALUES ($id, $int1, $int2, :string1, :string2)";
$pars = array(":string1"=>$string1,":string2"=>$string2);
$model->insert($sql,$pars);
now imagine this code with 20+ parameters.