简体   繁体   English

从上一个选择插入的PDO绑定参数

[英]PDO Binding parameters to insert from previous select

When executing the following script, I get an error at my INSERT for invalid parameter number: no parameters were bound. 当执行以下脚本时,我在INSERT上看到错误的参数号错误:没有绑定任何参数。

If I run the insert manually based on the previously selected values then it's fine. 如果我根据先前选择的值手动运行插入,那很好。 I know the syntax and relationships are all right. 我知道语法和关系都可以。 My issue is either in how I'm binding parameters or calling the execution. 我的问题是我如何绑定参数或调用执行。

Again, the first part of this script returns everything it needs, but my issue is occurring right at my insert statement, specifically erroring out after the insert statement is called. 同样,该脚本的第一部分返回了所需的所有内容,但是我的问题恰好发生在我的插入语句上,特别是在调用插入语句后出错。

Maybe It's still my lack of experience with PDO but from my docs it seems like this should work. 也许仍然是我缺乏PDO经验,但是从我的文档看来,这应该可行。

$order_ids = [];
while ($row = $ordStat->fetch(PDO::FETCH_ASSOC)) {
    $order_ids[] = $row['order_id'];
}
if (count($order_ids) > 0) {
    $placeholders = implode(',', array_fill(0, count($order_ids), '?'));
    $detailStatCheck = "
        SELECT 
             invnoc as INVOICE,
             fstatc as STATUS,
             cstnoc AS DEALER,
             framec AS FRAME,
             covr1c AS COVER,
             colr1c AS COLOR ,
             extd2d AS SHIPDATE,
             orqtyc AS QUANTITY
        FROM GPORPCFL
        WHERE invnoc IN ($placeholders)
    ";
    try {
        $detailCheck = $DB2conn->prepare($detailStatCheck);
        $detailRslt = $detailCheck->execute($order_ids);


        $count2 = $detailCheck->fetch();
        print_r($order_ids);
        print_r($count2);
    } catch(PDOException $ex) {
        echo "QUERY FAILED!: " .$ex->getMessage();
    }

    while ($row2 = $detailCheck->fetch(PDO::FETCH_ASSOC)){

        $insertPlacement = "
            INSERT INTO placements_new (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
                SELECT 
                     id, 
                     sku_group_id, 
                     :DEALER, 
                     DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
                     DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date, 
                     :QUANTITY,
                     :INVOICE  
                FROM skus s
            WHERE  s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR
        ";
        try{
            $insert = $MysqlConn->prepare($insertPlacement);
            $insertRslt = $insert->execute();
        }catch(PDOException $ex){
            echo "QUERY FAILED!!!: " . $ex->getMessage();
        }

    }
}

You need to actually bind the parameters (the error is helpful here: invalid parameter number: no parameters were bound ). 您实际上需要绑定参数(此错误对您有所帮助: invalid parameter number: no parameters were bound )。 You need to pass execute() an array containing the value to be assigned to each parameter: 您需要传递一个execute()数组,其中包含要分配给每个参数的值:

$values = [
    ":DEALER" => $row2["DEALER"],
    ":SHIPDATE" => $row2["SHIPDATE"],
    ":QUANTITY" => $row2["QUANTITY"],
    ":INVOICE" => $row2["INVOICE"],
    ":FRAME" => $row2["FRAME"],
    ":COVER" => $row2["COVER"],
    ":COLOR" => $row2["COLOR"],
];

And then 接着

$insertRslt = $insert->execute($values);

Notice that if you have ATTR_EMULATE_PREPARES set to false , you can't have one named parameter used two (or more) times, so you'll need to have :SHIPDATE as :SHIPDATE and :SHIPDATE2 and the respective additional value in the $values array. 请注意,如果将ATTR_EMULATE_PREPARES设置为false ,则不能使一个命名参数使用两次(或多次),因此您需要将:SHIPDATE:SHIPDATE:SHIPDATE2并在$values分别设置附加值阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM