在我的開發過程中,我正在做一個簡單的INSERT
,但我要回去並加入SQL保護並準備好語句。我收到的錯誤是:多個MySQL行INSERT失敗
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
這是我從別人的幫助中使用的代碼。
// foreach to set up variables
foreach ($json as $text) {
$uoid = mysql_real_escape_string($uoid);
$filename = mysql_real_escape_string($uoid.".jpg");
$filedate = mysql_real_escape_string($datetime);
$imagedesc = mysql_real_escape_string($desc);
// array of values
$insert[] = array($uoid,$filename,$filedate,$imagedesc);
}
function placeholders($text, $count=0, $separator=","){
$result = array();
if($count > 0){
for($x=0; $x<$count; $x++){
$result[] = $text;
}
}
return implode($separator, $result);
}
foreach($insert as $d){
$question_marks[] = '(' . placeholders('?', sizeof($d)) . ')';
}
$pdo = new PDO('mysql:dbname=photo_gallery;host=127.0.0.1', 'myuser', 'mypass');
$pdo->beginTransaction();
$sql = "INSERT INTO wp_gallery (" . implode(',', array_values($insert)) . ")
VALUES " . implode(',', $question_marks);
// reading output
echo $sql;
$stmt = $pdo->prepare($sql);
try {
$stmt->execute($insert[0]);
} catch (PDOException $e){
echo $e->getMessage();
}
$pdo->commit();
當我看SQL輸出:
INSERT INTO wp_gallery (10219776,10219776.jpg,my image description,2012-08-01 15:36:29)
VALUES (?,?,?,?),(?,?,?,?),(?,?,?,?),(?,?,?,?)
一切都只是它的外觀相匹配,但我仍然感到困惑,如何解決這個問題。有人能指出我做錯了什麼嗎?
你看不到錯誤? –
您需要列名稱。您將價值觀放在那裏,而不是替換問號。 – Ryan
在提供給社區之前,請檢查您的問題。 –