Pregunta

Tengo un PDO statement como sigue(AFAIK, PDO no necesita datos para ser escaped para la preparación):

$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();

Este trozo de código no hace nada.Pero sí genera un vertedero como este:

SQL: [91] INSERT INTO `errors`(`code`,`number`,`title`,`message`) VALUES( :code, :num, :title, :err )
Params:  0
¿Fue útil?

Solución

La información de tipo de un parámetro de enlace es un constant, y no un cadena:

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

Actualización

La matriz debe tener este aspecto:

$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 )
);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top