foreach recording several times in BD

0

Personal to making a product entry form (NF mirror) which has the option to clone the lines to include more products.

This form writes to 2 tables the 1st header of the NF and the 2nd saves the details where the product ID has the quantity the unit value the total value and the header ID for this use LAST_INSERT_ID ()

But I have a problem with the foreach as below when I have only one line in the form but when it has more than one item it gets crazy, writes several times in the database with incorrect data and from the second line it loses the LAST_INSERT_ID ()

Where am I going wrong?

    foreach($_POST["cat_id"] as $cat_id)
    foreach($_POST["qtd"] as $qtd)
    foreach($_POST["valor_unid"] as $valor_unid)
    foreach($_POST["valor_total"] as $valor_total)

mysql_query("INSERT INTO lc_detalhe (cat_id,qtd,valor_unid,valor_total,controle_id) values
    ('$cat_id','$qtd','$valor_unid','$valor_total',LAST_INSERT_ID())");


e o formulário

    <table border="0" cellpadding="2" cellspacing="4">
    <tr>
      <td class="bd_titulo">ID</td><td class="bd_titulo">Produto</td><td class="bd_titulo">Quantidade</td><td class="bd_titulo">Valor Unitario</td><td class="bd_titulo">Valor Total</td></tr>
  <tr class="linhas">
    <td><input type="text" name="id" style="text-align:center" disabled="true" /></td>
    <td><select name="cat_id[]">
<?php
while ($row=mysql_fetch_array($qr)){
?>
<option value="<?php echo $row['id']?>"><?php echo $row['nome']?></option>
<?php }?></td>
    <td>
      <input type="text" name="qtd[]" class="qtd" required name="qtd"
      style="text-align:center" />
    </td>
    <td> 
      <input name="valor_unid[]" type="text" required name="valor_unid"
      maxlength="30" 
      onblur="Calc(this)"
      class="valor_unid" />
    </td>
    <td>
      <input type="text" name="valor_total[]"
      class="vtotal"   readonly="readonly"
      style="text-align:center"/>
    </td>
    <td><a href="#" class="removerCampo" title="Remover linha"><img src="img/excluir.jpg" border="0" /></a></td>
  </tr>
  <tr><td colspan="4">
        <a href="#" class="adicionarCampo" title="Adicionar item"><img src="img/+.jpg" border="0" /></a>
    </td></tr>
  <tr>
<p><input type="submit" value="Cadastrar" /></p>
    
asked by anonymous 12.04.2017 / 16:02

0 answers