PHP PDO更新错误: number of bound variables does not match number of tokens

标签 php mysql sql pdo sql-update

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

我的假设是,这意味着 sql 字符串中占位符的数量(例如 :variable)与绑定(bind)元素的数量/名称不匹配。但是,我看不出问题出在哪里:

// 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 中运行更新...

最佳答案

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

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

于是变成了:

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

关于PHP PDO更新错误: number of bound variables does not match number of tokens,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23680689/

相关文章:

php - 在 MySQL 端或 PHP 端处理此查询更好吗?

SQL查询填充0的缺失数据

php - HTML PHP MYSQL 如何检查数据是否存在然后确认更新

php - 流或文件 "/storage/logs/laravel.log"无法打开 : failed to open stream: Permission denied

php - 有什么方法可以用 Laravel 检测数据库表是否存在

php - Laravel 显示类别计数不显示

mysql转oracle

sql - sp_send_dbmail 中的局部变量?

sql - 获取由另一列分组然后分组到另一列的列的总和

php - 在 Symfony 中编写 Twig 扩展时,为什么必须提供 getName()?