【问题标题】:PHP PDO update error: number of bound variables does not match number of tokensPHP PDO 更新错误:绑定变量的数量与令牌的数量不匹配
【发布时间】:2014-07-04 01:05:22
【问题描述】:

我在更新现有数据库记录时遇到问题。我不断收到PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

我的假设是,这意味着 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

当我用相应的数据替换 sql 占位符时,我在 mysql 中运行更新没有问题...

【问题讨论】:

    标签: php mysql sql pdo sql-update


    【解决方案1】:

    您在参数占位符周围使用引号,这是不对的。无论类型如何,都不应引用所有占位符。 PDO 只看到 7 个占位符中的 4 个,因为它无法识别引号中的那些,因此会出现不匹配错误。更改此行:

    $recipeSet[] = "`$field`=':$field'";
    

    这样就变成了:

    $recipeSet[] = "`$field`= :$field";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多