Php 不向准备语句中的X列插入值的逻辑

Php 不向准备语句中的X列插入值的逻辑,php,pdo,prepared-statement,Php,Pdo,Prepared Statement,我有一个表A,有X,Y,Z列 列Y和Z的默认值为“多样化”。 当php脚本交付userinput时,inputobject中包含的3个值中,Y列和Z列的两个值可能为NULL 我想创建一些PHP逻辑来评估输入并执行一个准备好的PDO查询,如果相应的输入为空字符串,则列Y和Z根本不受影响,因此mysql可以将它们设置为默认值 目前,我的PDO准备语句如下所示: $insertion = $connection->prepare("INSERT INTO products_tbl(produ

我有一个表A,有X,Y,Z列

列Y和Z的默认值为“多样化”。 当php脚本交付userinput时,inputobject中包含的3个值中,Y列和Z列的两个值可能为NULL

我想创建一些PHP逻辑来评估输入并执行一个准备好的PDO查询,如果相应的输入为空字符串,则列Y和Z根本不受影响,因此mysql可以将它们设置为默认值

目前,我的PDO准备语句如下所示:

  $insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category)
                                     VALUES(?,?,?)
                                     ");
$insertion->bindValue(1, $productDataInput["productNameInput"]);

  if($productDataInput["productManufacturerInput"] !== NULL){
     $insertion->bindValue(2, $productDataInput["productManufacturerInput"]);
  }

  if($productDataInput["productCategoryInput"] !== NULL){
     $insertion->bindValue(3, $productDataInput["productCategoryInput"]);
  }
我试图构建的控制实际插入的逻辑如下所示:

  $insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category)
                                     VALUES(?,?,?)
                                     ");
$insertion->bindValue(1, $productDataInput["productNameInput"]);

  if($productDataInput["productManufacturerInput"] !== NULL){
     $insertion->bindValue(2, $productDataInput["productManufacturerInput"]);
  }

  if($productDataInput["productCategoryInput"] !== NULL){
     $insertion->bindValue(3, $productDataInput["productCategoryInput"]);
  }
在这里,我得到以下错误:

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <b>D:\foundationtests\src\assets\php\addProducts.php</b> on line <b>38</b><br />
所以我想,这种准备一个查询的方法,需要插入3个值,但只接收1或2个值,是行不通的。 然而,我对准备好的语句还很陌生,如果不编写超级冗余代码,我真的不知道如何解决这个问题,我会为每个用例创建定制的准备好的语句,其中值2或3或两者都为空。这样的解决方案也不能很好地扩展,所以我想学习其他更有效、更简洁的方法^^

例如,我了解了DEFAULT是否能够触发将默认值设置为列?是否有办法在准备好的PDO语句中动态插入默认值?

您忘记了使用bindparam

现在只需将$var、var2、var3更改为您想要的变量即可

您可以使用它插入列的默认值,并将其放入带有IFNULL的测试中:

然后,当需要默认值时,可以将NULL传递给bindValue,即,可以删除if测试:

如果查询中使用的三个值是$productDataInput中的唯一值,则可以使用命名参数来进一步简化此操作

$insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category)
                                   VALUES(:productNameInput,
                                          IFNULL(:productManufacturerInput, DEFAULT(product_manufacturer)),
                                          IFNULL(:productCategoryInput, DEFAULT(product_category))
                                          )
                                   ");
$insertion->execute($productDataInput);

OP说有时候查询不应该收到3个值,或者我是误读了吗?他更新了问题我会再看你不必bindParam如果你是bindValueyeah,在他更新问题之前他显示了没有BindValuer的代码或者bindParam@GrumpyCrouton你是对的;只要$productDataInput中只有这3个值,您就可以转到查询的命名参数形式,它就可以正常工作。@GrumpyCrouton我已经根据您的建议进行了编辑。谢谢
$insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category)
                                   VALUES(:productNameInput,
                                          IFNULL(:productManufacturerInput, DEFAULT(product_manufacturer)),
                                          IFNULL(:productCategoryInput, DEFAULT(product_category))
                                          )
                                   ");
$insertion->execute($productDataInput);