Write many inputs to bd

Asked

Viewed 27 times

-2

<form method="POST" action="proc_editar_despacho.php">
                        <div class="form-group">
                        <div class="card">
                             <div class="card-body">        
                          <label for="exampleInputEmail1">ITEM</label>
                            <input type="text" name="item[]" class="form-control"  readonly value="<?php echo $row_despacho_itens['item']; ?>">
                            <br/>

                            <label for="exampleInputEmail1">DESCRIÇÃO</label>
                            <textarea class="form-control" name="descricao[]" rows="3"><?php echo $row_despacho_itens['descricao']; ?></textarea>
                            <br/>

                            <label for="exampleInputEmail1">UF</label>
                            <input type="text" name="uf[]" class="form-control"  value="<?php echo $row_despacho_itens['uf']; ?>">
                            <br/>

                            <label for="exampleInputEmail1">QTDE</label>
                            <input type="number" name="qtde[]" class="form-control" value="<?php echo $row_despacho_itens['qtd']; ?>">
                            <br/>

                            <label for="exampleInputEmail1">PU EM R$</label>
                            <input type="text" name="pu[]" class="form-control"  value="<?php echo $row_despacho_itens['preco_unitario']; ?>">
                            <br/>

                            <label for="exampleInputEmail1">PT EM R$</label>
                            <input type="text" name="pt[]" class="form-control" value="<?php echo $row_despacho_itens['preco_total']; ?>">
                                </div>
                            </div>
                            <?php }?>
                            <br/>
                           
                        </div>

                        <button type="submit" class="btn btn-primary">Editar</button>
                    </form>

PHP

for($i = 0; $i < $contador; $i ++){
    $result_item = "UPDATE tb_itens SET descricao='$descricao[$i]',  uf='$uf[$i]',  qtd='$qtde[$i]', preco_unitario ='$pu[$i]', preco_total='$pt[$i]'  WHERE id_despacho='$id_despacho' ";
    $resultado_item = mysqli_query($conn, $result_item);
}

if(mysqli_affected_rows($conn)){
    $_SESSION['msg'] = "<p style='color:green;'>Editado com sucesso</p>";
    header("Location: ../consultar.php");
}else{
    $_SESSION['msg'] = "<p style='color:red;'>Não foi editado </p>";
    header("Location: ../login.php");
}

Gives error only to change the last record

  • have you tried debugging to find out what causes this error? or tried to grab the command update and run straight into the bank?

1 answer

0

Thank you so much for the tip rLinhares, already get here. I did it that way.

$dados = $_POST;
$count = count($_POST['item']);
//echo "<pre>";
//var_dump($dados);
//echo "</pre>";

for ($i=0; $i < $count; $i++) { 
    $result_itens = " INSERT INTO tb_itens(id_ata, descricao, qtd, preco_unitario, preco_total, uf, item) VALUES ('{$_POST['id_ata'][$i]}', '{$_POST['descricao'][$i]}', '{$_POST['qtde'][$i]}', '{$_POST['pu'][$i]}', '{$_POST['pt'][$i]}', '{$_POST['uf'][$i]}', '{$_POST['item'][$i]}') ";
    $resultado_itens = mysqli_query($conn, $result_itens);
}

Browser other questions tagged

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