PDO更新不替换预准备语句中的占位符

时间:2020-12-21 12:02:27

Im trying to update a table with the following code. If I change WHERE temp_booking_id = ':temp_booking_id'"); to use the actual, current session temp_id, the query will run, but adds the placeholders into the table (e.g.: check-out) as the value.

我试图用以下代码更新表。如果我更改WHERE temp_booking_id =':temp_booking_id'“);要使用实际的当前会话temp_id,查询将运行,但会将占位符添加到表中(例如:签出)作为值。

$data is holding the correct values, but is not replacing the placeholders.

$ data保持正确的值,但不替换占位符。

Been staring at this for hours and can't for the life of me work out what the problem is, and looked around but haven't found a solution.

一直盯着这几个小时,不能为我的生活解决问题所在,并环顾四周,但没有找到解决方案。

PDOStatement:errorInfo() is returning

PDOStatement:errorInfo()正在返回

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

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

and if I remove the inverted commas around the placeholders, it returns

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

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

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

Any ideas?

有任何想法吗?

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();
}

1 个解决方案

#1


5  

Hmmm, it seems that your SQL statement doesn't require the single quotes. For example, you can try running this block instead:

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

   $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");

Check out the PHP manual on PDO prepared statements: http://www.php.net/manual/en/pdo.prepared-statements.php It looks like it here that the quotes are not necessary around the named placeholders.

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

Also, try following their example where they use the bindParam() method:

另外,尝试按照他们使用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);

When you are ready to execute, you can run the following line:

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

$STH->execute();

Check it out and see if binding the parameters is what you're looking for.

检查一下,看看参数的绑定是否符合您的要求。

#1


5  

Hmmm, it seems that your SQL statement doesn't require the single quotes. For example, you can try running this block instead:

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

   $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");

Check out the PHP manual on PDO prepared statements: http://www.php.net/manual/en/pdo.prepared-statements.php It looks like it here that the quotes are not necessary around the named placeholders.

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

Also, try following their example where they use the bindParam() method:

另外,尝试按照他们使用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);

When you are ready to execute, you can run the following line:

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

$STH->execute();

Check it out and see if binding the parameters is what you're looking for.

检查一下,看看参数的绑定是否符合您的要求。