1
我在更新現有數據庫記錄時遇到問題。我一直得到PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
。PHP PDO更新錯誤:綁定變量的數量與令牌數量不匹配
我的假設是,這意味着sql字符串中佔位符的數量(例如:variable
)與綁定元素的數量/名稱不匹配。然而,我不能看到我的問題是:
// arrays for text and numeric form data
$numfield_names = array("activeTime", "totalTime", "servings");
$textfield_names = array("activeTimeDesc", "totalTimeDesc", "yield");
// get record id
$rid = filter_input(INPUT_GET, 'rid', FILTER_SANITIZE_NUMBER_INT);
// get text & num fields
$textFields = array();
$numFields = array();
foreach($textfield_names as $n){
$textFields[$n] = filter_input(INPUT_GET, $n, FILTER_SANITIZE_STRING); }
foreach($numfield_names as $n){
$numFields[$n] = filter_input(INPUT_GET, $n, FILTER_SANITIZE_NUMBER_INT); }
try{
include "dbConnect.php";
// update recipe data
$recipeSet = array();
$recipeData = array('id'=>$rid);
$recipeFields = array_merge($numFields, $textFields);
$recipeFields['id'] = $rid;
foreach($numFields as $field => $val){
$recipeSet[] = "`$field`=:$field";
$recipeData[$field] = $val;
}
foreach($textFields as $field => $val){
$recipeSet[] = "`$field`=':$field'";
$recipeData[$field] = $val;
}
print_r($numFields);
print_r($textFields);
$recipeSetStr = trim(implode(',', $recipeSet) , ',');
$sql = "UPDATE `recipes` SET " . $recipeSetStr . " WHERE `id`=:id";
print_r($sql);
$stmt = $conn->prepare($sql);
$stmt->bindValue(":activeTime", $recipeData['activeTime']);
$stmt->bindValue(":totalTime", $recipeData['totalTime']);
$stmt->bindValue(":servings", $recipeData['servings']);
$stmt->bindValue(":activeTimeDesc", $recipeData['activeTimeDesc']);
$stmt->bindValue(":totalTimeDesc", $recipeData['totalTimeDesc']);
$stmt->bindValue(":yield", $recipeData['yield']);
$stmt->bindValue(":id", $recipeData['id']);
$stmt->execute();
} catch (PDOException $e) {
// return error - I'm only hitting this...
echo $e->xdebug_message;
}
輸出如下:
Array (
[activeTime] => 10
[totalTime] => 10
[servings] => 32
)
Array (
[activeTimeDesc] => 10 minutes
[totalTimeDesc] => 10 minutes (plus 1 day to make yogurt cheese)
[yield] => 2 cups
)
UPDATE `recipes` SET `activeTime`=:activeTime,`totalTime`=:totalTime,`servings`=:servings,`activeTimeDesc`=':activeTimeDesc',`totalTimeDesc`=':totalTimeDesc',`yield`=':yield' WHERE `id`=:id
(!) PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /sites/kaleUI/setRecipe.php on line 77 Call Stack #TimeMemoryFunctionLocation 10.0008695680{main}()../setRecipe.php:0 20.0267722104execute ()../setRecipe.php:77
當我與cooresponding數據替代SQL佔位符我有運行在更新沒有問題mysql ...