Question

I have a PDO statement as follows(AFAIK, PDO doesn't need data to be escaped for preparation):

$insert = "INSERT INTO `errors`(`code`,`number`,`title`,`message`) VALUES( :code, :num, :title, :err )";
$arr = Array(
    Array( ':code', $_POST['code'], 'PDO::PARAM_STR' ),
    Array( ':num', $_POST['number'], 'PDO::PARAM_INT' ),
    Array( ':title', $_POST['title'], 'PDO::PARAM_STR' ),
    Array( ':err', htmlentities( str_replace("\n", "<br />", $_POST['error']), ENT_HTML5, 'UTF-8' ), 'PDO::PARAM_STR' )
);
$stmt = $conn->prepare($insert);
foreach( $arr as $a ) {
    $stmt->bindValue( $a[0], $a[1], $a[2] );
}
$stmt->execute();
$stmt->debugDumpParams();

This piece of code does nothing at all. But it does generate a dump like this:

SQL: [91] INSERT INTO `errors`(`code`,`number`,`title`,`message`) VALUES( :code, :num, :title, :err )
Params:  0
Was it helpful?

Solution

The type info of a bind parameter is a constant, and not a string:

Array( ':code', $_POST['code'], PDO::PARAM_STR )
//                             ^              ^ no quotes

Update

The array should look like this:

$arr = Array(
    Array( ':code', $_POST['code'], PDO::PARAM_STR ),
    Array( ':num', $_POST['number'], PDO::PARAM_INT ),
    Array( ':title', $_POST['title'], PDO::PARAM_STR ),
    Array( ':err', htmlentities( str_replace("\n", "<br />", $_POST['error']), ENT_HTML5, 'UTF-8' ), PDO::PARAM_STR )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top