Recording only the last line in the DB

1

To make a form to input products into stock (NF mirror) so I am using a javascript code to clone the rows.

So, okay, the problem is when it's time to register because it only registers the last line of products, will it be a problem in my insert, form or java?

To break the head to mount array / foreach and I do not think where the problem is, please help me ....

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())"); 

Part of the code

<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>

I think I'm getting somewhere..kkkk

Now he is recording too much in the DB. when it has only one line it writes correctly but when it has more than one line it triples the lines in the DB and does not respect the LAST_INSERT_ID ()

Looks like I've mounted the insert:

    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())");

look how the form fields are:

 <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>

Look how he recorded in the DB: he lost control_id from the 3rd record that comes from LAST_INSERT_ID (), the values do not match what was typed and inserted several times the same record

 Textos completos   id  controle_id  cat_id qtd valor_unid       valor_total
 Edita Edita    Copiar Copiar   Apagar Apagar   1   1   533 1630     1.00               1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   2   1   533 1630     1.00   16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   3   2   533 1630     0.01   1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   4   3   533 1630            0.01    16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   5   4   533 1631    1.00    1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   6   5   533 1631    1.00    16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   7   6   533 1631    0.01    1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   8   7   533 1631    0.01    16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   9   8   534 1630    1.00    1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   10  9   534 1630    1.00    16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   11  10  534 1630    0.01    1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   12  11  534 1630    0.01    16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   13  12  534 1631    1.00    1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   14  13  534 1631    1.00    16.31
 Edita Edita    Copiar Copiar   Apagar Apagar   15  14  534 1631    0.01    1630.00
 Edita Edita    Copiar Copiar   Apagar Apagar   16  15  534 1631    0.01    16.31
    
asked by anonymous 11.04.2017 / 19:44

1 answer

0

Insert example with foreach

if (isset($_POST['submit'])) {

  $i = 0;
  foreach ($_POST as $val) {
     $qtd = $_POST['qtd'][$i];
     $valor_unid = $_POST['valor_unid'][$i];
     ........................
     ........................

     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())"); 

   $i++;
  } 
}
    
11.04.2017 / 20:35