php - PDO 更新不替换准备好的语句中的占位符

标签 php mysql pdo

我正在尝试使用以下代码更新表格。如果我更改 WHERE temp_booking_id = ':temp_booking_id'"); 以使用实际的当前 session temp_id,查询将运行,但将占位符添加到表中(例如: check-out) 作为值。

$data 保存了正确的值,但没有替换占位符。

我盯着这个看了好几个小时,但终究无法找出问题所在,环顾四周,但没有找到解决方案。

PDOStatement:errorInfo() 正在返回

PDOStatement::errorInfo(): Array ( [0] => 00000 )

如果我删除占位符周围的引号,它会返回

PDOStatement::errorInfo(): Array ( [0] => HY093 )

有什么想法吗?

try {
  $data = array(
    'temp_booking_id' => $_SESSION['temp_id'],
    'check_in' => $in,
    'check_out' => $out, 
    'adults' => $a,
    'children1' => $c1,
    'children2' => $c2,
    'infants' => $i,
    'cots' => $c,
    'promo_code' => $pc
 );

 $STH = $DBH->prepare("UPDATE b_temp_booking 
   SET check_in = ':check_in',
   check_out = ':check_out',
   adults = ':adults',
   children1 = ':children1',
   children2 = ':children2',
   infants = ':infants',
   cots = ':cots',
   promo_code = ':promo_code' 
   WHERE temp_booking_id = ':temp_booking_id'");

 $STH->execute($data);

 echo "\nPDOStatement::errorInfo():\n";
 $arr = $STH->errorInfo();
 print_r($arr);

} catch(PDOException $e) {
  echo 'ERROR: ' . $e->getMessage();
}

最佳答案

嗯,看来你的 SQL 语句不需要单引号。例如,您可以尝试运行此 block :

   $STH = $DBH->prepare("UPDATE b_temp_booking 
   SET check_in = :check_in,
   check_out = :check_out,
   adults = :adults,
   children1 = :children1,
   children2 = :children2,
   infants = :infants,
   cots = :cots,
   promo_code = :promo_code 
   WHERE temp_booking_id = :temp_booking_id");

查看有关 PDO 准备语句的 PHP 手册:http://www.php.net/manual/en/pdo.prepared-statements.php 在这里,命名占位符周围似乎不需要引号。

此外,尝试按照他们使用 bindParam() 方法的示例进行操作:

$STH->bindParam(':temp_booking_id', $temp_booking_id);

$temp_booking_id = $_SESSION['temp_id']; // Not sure how binding the environment variable will work, so decoupling it.

$STH->bindParam(':check_in', $in);
$STH->bindParam(':check_out', $out); 
$STH->bindParam(':adults', $a);
$STH->bindParam(':children1', $c1);
$STH->bindParam(':children2', $c2);
$STH->bindParam(':infants', $i);
$STH->bindParam(':cots', $c);
$STH->bindParam(':promo_code', $pc);

当您准备好执行时,您可以运行以下行:

$STH->execute();

检查一下,看看绑定(bind)参数是否是您要找的。

关于php - PDO 更新不替换准备好的语句中的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11332987/

相关文章:

php - 如何使用 Dockerfile 以基于 Ubuntu 镜像的 php-fpm 启动 Nginx?

php - 使用 group by 为报告(订单报告)编写单个 mysql 查询

javascript - 需要在一页php中显示输入和输出?

MySQL:在子子查询上使用主查询变量

mysql - SQL COUNT(*) 是一个 JOIN 语句,而不是嵌套 SELECT 语句中的 COUNT(*),以获得更好的性能

php - PDO var_dump 变得错误

php - 如何摆脱 Laravel 中过多的 if 语句?

php - 在检索 MySQL 表时克服 PHP 内存耗尽或执行时间错误

php - PDO 使用正确的值但将错误的值插入数据库

php - UTF-8编码问题