Make an update of several fields and values in the same table

2

I'm making a very simple inventory control system, and in the edit part of the products, I'm doing it this way: I query in a database, and clear text fields with the name of the products and the respective quantity , as in the attached image:

Inthiswaytheusercouldchangeeverythinghewanted,andwhenheclickedonsend,makeanupdateofallthefieldsinthebank,accordingtotherightorder.

Myquestionis:Howtodothis?

Currentlymyformlookslikethis:

<formaction="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 by values, it works correctly. I think I'm missing the idea of passing an array, but I do not know how to do it. I do not know if I'm doing the right thing, or if I'm doing the right thing. If you can help me, I'll be grateful, thank you.

    
asked by anonymous 15.07.2015 / 00:11

1 answer

2

Your problem is in the $dados array and in iteration of the 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. In order for your foreach to work the way you did, 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++; } ?>

In this way, you will only have one array, $txtDados . In the update_pcs.php file, change the $ data variable to $dados = $_POST['txtDados'];

    
15.07.2015 / 02:47