在准备好的语句中不向列 X 插入值的逻辑
Logic to NOT insert a value to column X in a prepared statement
所以我有 table A,其中包含 X、Y、Z 列。
Y、Z 列的默认值为 "diverse"。
当 php 脚本传递用户输入时,输入对象中包含的 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"]);
}
在这里,我得到以下错误:
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 或两者都为空。这样的解决方案也没有真正扩展 "well" 所以我想学习其他更有效和简洁的方法...^^
例如,我了解了 DEFAULT() 能够触发将默认值设置为列?有什么方法可以在准备好的 PDO 语句中动态插入 DEFAULT 吗?
您忘记了 bindparam
$insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category) VALUES(:prod_name , :prod_manufacturer, :prod_category)");
$insertion->bindParam(':prod_name', $var);
$insertion->bindParam(':prod_manufacturer', $var2);
$insertion->bindParam(':prod_category', $var3);
$insertion->->execute();
现在只需将 $var、var2、var3 更改为您想要的变量
您可以使用 DEFAULT()
为列插入默认值,将其置于 IFNULL
:
的测试中
$insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category)
VALUES(?,
IFNULL(?, DEFAULT(product_manufacturer)),
IFNULL(?, DEFAULT(product_category))
)
");
然后您可以在需要默认值时将 NULL
传递给 bindValue
,即您可以删除 if
测试:
$insertion->bindValue(1, $productDataInput["productNameInput"]);
$insertion->bindValue(2, $productDataInput["productManufacturerInput"]);
$insertion->bindValue(3, $productDataInput["productCategoryInput"]);
如果查询中使用的三个值是仅 $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);
所以我有 table A,其中包含 X、Y、Z 列。
Y、Z 列的默认值为 "diverse"。 当 php 脚本传递用户输入时,输入对象中包含的 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"]);
}
在这里,我得到以下错误:
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 或两者都为空。这样的解决方案也没有真正扩展 "well" 所以我想学习其他更有效和简洁的方法...^^
例如,我了解了 DEFAULT() 能够触发将默认值设置为列?有什么方法可以在准备好的 PDO 语句中动态插入 DEFAULT 吗?
您忘记了 bindparam
$insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category) VALUES(:prod_name , :prod_manufacturer, :prod_category)");
$insertion->bindParam(':prod_name', $var);
$insertion->bindParam(':prod_manufacturer', $var2);
$insertion->bindParam(':prod_category', $var3);
$insertion->->execute();
现在只需将 $var、var2、var3 更改为您想要的变量
您可以使用 DEFAULT()
为列插入默认值,将其置于 IFNULL
:
$insertion = $connection->prepare("INSERT INTO products_tbl(product_name, product_manufacturer, product_category)
VALUES(?,
IFNULL(?, DEFAULT(product_manufacturer)),
IFNULL(?, DEFAULT(product_category))
)
");
然后您可以在需要默认值时将 NULL
传递给 bindValue
,即您可以删除 if
测试:
$insertion->bindValue(1, $productDataInput["productNameInput"]);
$insertion->bindValue(2, $productDataInput["productManufacturerInput"]);
$insertion->bindValue(3, $productDataInput["productCategoryInput"]);
如果查询中使用的三个值是仅 $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);