Question

j'ai un PDO statement comme suit(Autant que je sache, PDO n'a pas besoin de données pour être escaped pour la préparation) :

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

Ce morceau de code ne fait rien du tout.Mais cela génère un dump comme celui-ci :

SQL: [91] INSERT INTO `errors`(`code`,`number`,`title`,`message`) VALUES( :code, :num, :title, :err )
Params:  0
Était-ce utile?

La solution

Les informations de type d'un paramètre de liaison sont un constant, et non un chaîne:

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

Mise à jour

Le tableau devrait ressembler à ceci :

$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 )
);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top