无法从foreach插入第一条记录

时间:2017-02-06 13:57:45

标签: php arrays mysqli foreach

我遇到了foreach循环的问题,这不会影响数据库中的第一条记录,而我可以看到它打印出所有记录。

这是我使用的代码:

            //Now we get costs in% and absolute
        $number = 0;
        $numberText = "";
        $numberName = "";
        foreach($cost_array as $key => $value){
          //Here we select only costs with % so we could calculate percentage
          if(strcmp($value["cost_measure"],"Percent") === 0){
            //$number .= getPercentOfNumber($local,$value["cost_amount"])."<br>";
            $number = getPercentOfNumber($local,$value["cost_amount"]);
            //$numberText.= $value["cost_amount"]." %<br>";
            $numberText = $value["cost_amount"];
            //$numberName .= "cost_name ".$value["cost_name"]."<br>";
            $numberName = $value["cost_name"];

            //Insert calculated costs in DB
            if ($stmt = $conn_mysqli -> prepare("INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
                $stmt -> bind_param("iiissssss", $calculation_id, $costs_id, $user_id, $create_time, $local, $numberText, $value['cost_measure'], $number, $numberName);
                $stmt -> execute();
                $stmt -> close();
            }
            $sql = "INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES ($calculation_id, $costs_id, $user_id, $create_time, $local, $numberText, ".$value['cost_measure'].", $number, $numberName)";
            echo $sql."<br>";
            }
}

当我显示我的查询以查看应插入的内容时,我得到以下结果:

INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 0.50, Percent, 40.3045, Trosak prijevoz)
INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 0.00, Percent, 0, Carina)
INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 0.15, Percent, 12.09135, Banka)
INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 25.00, Percent, 2015.225, PDV)

在DB中我得到了这个结果:

INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 0.00, Percent, 0, Carina)
INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 0.15, Percent, 12.09135, Banka)
INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (190, 5, 10, 2017-02-06 14:17:58, 8060.9, 25.00, Percent, 2015.225, PDV)

为什么第一条记录未插入数据库?我无法弄清楚。

0 个答案:

没有答案