Update several fields and values in the same table

Asked

Viewed 2,360 times

2

I’m making a very simple system for inventory control, and in the part of editing the products, I’m doing it this way: I do a query in a database, and I list text fields with the name of the products and the respective quantity, as in the attached image: Sistema de estoque

This way the user could change everything he wanted, and when clicking submit, make an update of all fields in the database, according to the right order.

My question is: How to do this ?

Currently my formulary is like this:

<form action="update_pcs.php" method="POST">
            <label>Peça</label>
            <label for="txtQtd" id="qtd">Qtd.</label>
            <br/>
                <?php foreach($rtn as $pcs){ ?>
                        <input type="text" name="txtNome[]" id="txtNome" value="<?=$pcs['pc_nome']?>" />
                        <input type="text" name="txtQtd[]" id="txtQtd" value="<?=$pcs['num']?>"/>
                        <input type="hidden" name="txtId[]" id="txtId" value="<?=$pcs['id']?>" />
                        <br />
                        <br />
                <?php } ?>
            <br />
            <br />
            <input type="submit" value="Enviar" name="btnEnvia" />
    </form>

And the update_pcs.php file, which should update:

<?php

    include_once 'mdl.php';
    $conexao = new modelDB();

    $qtd = $_POST['txtQtd'];
    $nom = $_POST['txtNome'];
    $id = $_POST['txtId'];

    $dados = array('nome'=>$nom,
                    'qtd'=>$qtd,
                    'id'=>$id);

    /*isso faz com que o campo nome de $dados seja um array, qtd outro array e id outro*/
    foreach($dados as $dado){

        /* Atualmente estou fazendo desta forma, mas não está funcionando */
        $nomeAt = $dado['nome'];
        $qtdAt = $dado['qtd'];
        $id = $dado['id'];  

        $conexao->alteraDb("update pcs_estq set pc_nome ='{$nomeAt}', num = '{$qtdAt}' where id = '{$idAt}'");
    }

The function is correct because when I change the variables for values, it works correctly. I believe you’re mistaken in the idea of passing an array, but I don’t know how to do it. I don’t know if I’m doing it the right way, or if I’m doing it the best way. If you can help me I’ll be grateful, thank you.

1 answer

3


Your problem is in the array $dados and its iteration by foreach. The variables$qtd, $nom and $id are arrays. Just when you do:

$dados = array('nome'=>$nom, 'qtd'=>$qtd, 'id'=>$id);

You are creating an array array. For your foreach to work the way you wrote, modify the form this way:

<?php $count = 0; foreach($rtn as $pcs){ ?>
                    <input type="text" name="txtDados[<?= $count ?>][nome]" id="txtNome" value="<?=$pcs['pc_nome']?>" />
                    <input type="text" name="txtDados[<?= $count ?>][qtd]" id="txtQtd" value="<?=$pcs['num']?>"/>
                    <input type="hidden" name="txtDados[<?= $count ?>][id]" id="txtId" value="<?=$pcs['id']?>" />
                    <br />
                    <br />
            <?php $count++; } ?>

That way, you will only have an array, $txtDados. In the archive update_pcs.php, change the variable $given to $dados = $_POST['txtDados'];

  • 1

    I found a similar solution to yours in the stack in English, but yours was slightly better, thank you and congratulations ! I hope one day I can reciprocate.

Browser other questions tagged

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