【问题标题】:Two bindParam in same string values相同字符串值中的两个 bindParam
【发布时间】:2021-09-01 14:47:08
【问题描述】:

提前为我的英语感到抱歉。

我在使用 PDO bindParam 时遇到“问题”。


$sql_fact_1 = "INSERT INTO factures_interventions (id_client, id_facture, id_cat, detail_intervention, prix_intervention, date_intervention, etat_intervention)
VALUES (:id_client, '0', '3', 'Renouvellement automatique annuel (:date_fin) du compte :compte', :tarif, :date_actuelle, '0' )";

$req = $bdd->prepare($sql_fact_1);

$req->bindParam('id_client', $client_1['id'], PDO::PARAM_INT);
$req->bindParam('date_fin', $dateRenou, PDO::PARAM_STR);
$req->bindParam('compte', $compte_1['compte'], PDO::PARAM_STR);
$req->bindParam('tarif', $resultat_form_1['tarif'], PDO::PARAM_STR);
$req->bindParam('date_actuelle', $date, PDO::PARAM_STR);

$req->execute();
$req->closeCursor();

这里的问题

'Renouvellement automatique annuel (:date_fin) du compte :compte'

是我试图在一个短语中输入两个绑定但我得到一个错误:

致命错误:未捕获的 PDOException:SQLSTATE[HY093]:无效的参数号:绑定变量的数量与标记的数量不匹配

'Renouvellement automatique annuel (':date_fin') du compte ':compte''

这样,它可以工作,但我的行中有报价。 :(

喜欢:

Renouvellement automatique annuel ('2022/09/01') du compte 'moncompte.fr'

如何去掉引号?

【问题讨论】:

  • 字符串值的中间不能有占位符。在查询之外构建您的字符串,然后插入字符串本身。
  • 或者使用 CONCAT 从查询中的静态部分和占位符值中创建值。
  • 谢谢,确实好多了^^

标签: php mysql pdo


【解决方案1】:

您不能在带引号的字符串中包含参数。你必须建立你的字符串并插入:

$sql_fact_1 = "INSERT INTO factures_interventions (id_client, id_facture, id_cat, detail_intervention, prix_intervention, date_intervention, etat_intervention)
VALUES (:id_client, '0', '3', :renouvellement, :tarif, :date_actuelle, '0' )";

$renouvellement = "Renouvellement automatique annuel ($dateRenou) du compte ".$compte_1['compte'];
$req = $bdd->prepare($sql_fact_1);

$req->bindParam('id_client', $client_1['id'], PDO::PARAM_INT);
$req->bindParam('renouvellement', $renouvellement, PDO::PARAM_STR);
$req->bindParam('tarif', $resultat_form_1['tarif'], PDO::PARAM_STR);
$req->bindParam('date_actuelle', $date, PDO::PARAM_STR);

【讨论】:

  • 谢谢,确实好多了^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
相关资源
最近更新 更多