title: Failed to insert first record from foreach
body: <p>I'm having a problem with the foreach loop which is not isnerting first record in DB while I can see it print's out all the records. </p>
  
  <p>This is the code I'm using:</p>
  
  <pre><code>            //Now we get costs in% and absolute
          $number = 0;
          $numberText = "";
          $numberName = "";
          foreach($cost_array as $key =&gt; $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"])."&lt;br&gt;";
              $number = getPercentOfNumber($local,$value["cost_amount"]);
              //$numberText.= $value["cost_amount"]." %&lt;br&gt;";
              $numberText = $value["cost_amount"];
              //$numberName .= "cost_name ".$value["cost_name"]."&lt;br&gt;";
              $numberName = $value["cost_name"];
  
              //Insert calculated costs in DB
              if ($stmt = $conn_mysqli -&gt; prepare("INSERT INTO calculations_cost (calculation_id, costs_id, user_id, time, original_number, amount, measure, calculated_cost, name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
                  $stmt -&gt; bind_param("iiissssss", $calculation_id, $costs_id, $user_id, $create_time, $local, $numberText, $value['cost_measure'], $number, $numberName);
                  $stmt -&gt; execute();
                  $stmt -&gt; 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."&lt;br&gt;";
              }
  }
  </code></pre>
  
  <p>When I display my query to see what should be inserted, I get this result:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>In DB I get this result:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Why first record is not inserted in DB? I can't figure it out.</p>