Domanda

Ho un PDO statement come segue ( AFAIK , PDO non ha bisogno di dati per essere escaped per la preparazione):

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

Questo pezzo di codice non fa nulla.Ma genera una scarica come questa:

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

È stato utile?

Soluzione

Le informazioni del tipo di un parametro bind è un constant e non a string :

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

Aggiornamento

L'array dovrebbe assomigliare a questo:

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top