Recording only the last line in the BD

Asked

Viewed 130 times

1

I am making a form to enter products in stock (NF mirror) for this I am using a Cód in javascript to clone the lines.

So far so good, the problem and the time to have register because it only registers the last line of products, will be problem in my Internet, form or java?

I’m breaking my head to mount array/foreach and I can’t find 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 Cód.

<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’s recording too much in the comic book. when it has only one line it records right but when it has more than one line it triples the lines in the BD and does not respect the LAST_INSERT_ID()

Look how I rode the Indsert:

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

looks like the form fields:

 <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 wrote in the BD: he lost the controle_id from the 3rd record coming 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
  • shows the array/foreach you developed?

  • Note the fact that the input name='qtd' does not have the characters [] in its name and, in this way, would be unique when making the request. In fact, both the input name='qtd'how much input name='valor_unid' have the attribute name duplicate.

  • Finally, no longer use the functions mysql_* PHP, read more about this here, here and here.

  • read this answer here.

1 answer

0

Example of Insert 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++;
  } 
}

Browser other questions tagged

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