foreach recording several times in the BD

Asked

Viewed 44 times

0

Staff are making a product input form (NF mirror) that has the option to clone the lines to include more products.

This form records in 2 tables 1st NF header and the 2nd saves the details where the product ID has the quantity the unit value the total value and the header ID for that use the LAST_INSERT_ID()

Only I’m having a problem I rode the foreach as below when I have only one line in the form but when more than one item he gets crazy, record several times in the database with incorrect data and from the second line he 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>
  • Do not create repeated questions here in the community, please. You have already posted this problem, so continue with the discussion there. Repeatedly recreating will only pollute the site. See your other question: Recording only the last line in the BD

  • is that that problem has been solved, now I have another problem

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.