É este bindValue errado?
-
12-12-2019 - |
Pergunta
Eu tenho um PDO statement
como segue(AFAIK, PDO não precisa de dados para ser escaped
para a preparação):
$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 pedaço de código faz nada.Mas ele faz gerar um despejo de como este:
SQL: [91] INSERT INTO `errors`(`code`,`number`,`title`,`message`) VALUES( :code, :num, :title, :err )
Params: 0
Solução
As informações de tipo de um parâmetro de ligação é um constant
, e não um cadeia:
Array( ':code', $_POST['code'], PDO::PARAM_STR )
// ^ ^ no quotes
Atualização
A matriz deve olhar como este:
$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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow