Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法从foreach插入第一条记录_Php_Arrays_Mysqli_Foreach - Fatal编程技术网

Php 无法从foreach插入第一条记录

Php 无法从foreach插入第一条记录,php,arrays,mysqli,foreach,Php,Arrays,Mysqli,Foreach,我在foreach循环中遇到了一个问题,它没有关闭DB中的第一条记录,而我可以看到它打印出了所有记录 这是我正在使用的代码: //Now we get costs in% and absolute $number = 0; $numberText = ""; $numberName = ""; foreach($cost_array as $key => $value){ //Her

我在foreach循环中遇到了一个问题,它没有关闭DB中的第一条记录,而我可以看到它打印出了所有记录

这是我正在使用的代码:

            //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>";
            }
}
在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.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)
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)

为什么第一条记录不插入数据库中?我想不出来。

也许您在第一次插入时有错误,如果出现错误,您是否尝试打印错误;像这样:
f(!$stmt->execute())echo“error:”$mysqli->错误否,没有错误。它不是插入没有任何错误的第一条记录。我不明白为什么。第一条记录没有什么特别的地方会引发错误。您是否尝试在数据库中手动执行这四个插入?是的,它没有出现任何问题。好的,我设法得到了一些错误,它说:注意:C:\wamp\www\pma\calculation\custom\u-based\u-method.php中的未定义偏移量:9,第177行,这是什么意思?