要从先前的选择

时间:2018-03-04 05:35:26

标签: php mysql pdo db2

执行以下脚本时,我的INSERT中出现错误,参数号无效:没有绑定任何参数。

如果我根据之前选择的值手动运行插入,那么它没问题。我知道语法和关系都没问题。我的问题是我如何绑定参数或调用执行。

同样,这个脚本的第一部分返回它需要的所有内容,但我的问题发生在我的insert语句中,特别是在调用insert语句后出错。

也许我仍然缺乏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();
        }

    }
}

1 个答案:

答案 0 :(得分:2)

您需要实际绑定参数(错误在此处有用:invalid parameter number: no parameters were bound)。您需要传递execute()一个数组,该数组包含要分配给每个参数的值:

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

然后

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

请注意,如果ATTR_EMULATE_PREPARES设置为false,则一个命名参数的使用时间不能超过两次(或更多次),因此您需要:SHIPDATE作为:SHIPDATE :SHIPDATE2$values以及package weightTest; //import DecimalFormat; import javax.swing.*; import java.lang.Object; import java.awt.Component; import java.awt.Container; public class weightTestClass { enter code here public static void main(String [] args) { int startingPounds = 0; int endingPounds = 0; startingPounds = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the starting weight in pounds")); endingPounds= Integer.parseInt(JOp tionPane.showInputDialog(null, "Enter the end weight in pounds")); //Scanner kb = new Scanner(System.in); //int startingPounds= kb.nextInt(); System.out.println("\nPounds || Kilograms"); System.out.println("---------------------"); if(startingPounds>endingPounds) { JOptionPane.showMessageDialog(null, "you have an invalid value"); //System.out.println("you have an invalid value"); } else { for(int i=startingPounds;i<=endingPounds; i++) { System.out.println(""); //System.out.print(i+" lb" + " " + i*0.454+" kg"); } JOptionPane.showMessageDialog(null, i+" lb" + " " + i*0.454+" kg"); // System.out.println("the weight in kilograms is "+ kilograms); } } } 数组中的相应附加值。