1

I'm making a very simple stock control system. In the page that the user will be able to do the updates, I'm listing all the contents in the table and putting everything in text fields, like the image.Stock control example

So, the user should just change what he wants and when he clicks on submit, it would update every row in the table, with its respective value. But my code isn't working. I don't know how to make this kind of update and I'm sure it's wrong, because it doesn't even work. Here my form:

<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 my file update_pcs.php, which should do the update in the table.

<?php

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

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

    /*This makes the $dados array keep in the value ['nome'] another array, the value ['qtd'] another array and ['id'] another array*/
    $dados = array('nome'=>$nom,
                    'qtd'=>$qtd,
                    'id'=>$id);


    foreach($dados as $dado){

        /* I'm doing it that way but it isn't working */
        $nomeAt = $dado['nome'];
        $qtdAt = $dado['qtd'];
        $id = $dado['id'];  

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

My function must be right because when I change the php variables for values, it works. How could I make this right?

1 Answer 1

3

With sending array names in your html form, you will receive array post variables.

First change your form like below to help posting more appropriate data.

<input type="text" name="values[<?=$pcs['id']?>][Nome]" id="txtNome" value="<?=$pcs['pc_nome']?>" />
<input type="text" name="values[<?=$pcs['id']?>][Qtd]" id="txtQtd" value="<?=$pcs['num']?>"/>
<input type="hidden" name="values[<?=$pcs['id']?>][id]" id="txtId" value="<?=$pcs['id']?>" />

Actually this way you don't need to send id but i am sending for simple explanation.

Now you post, you'll receive an array like :

values => array (
                 1 => array ( 
                            'id'=> 1, 
                            'Nome' => 'Some text', 
                            'Qtd' => 1
                          ),
                 2 => .... 
                )

Now you can get your data and insert your db.

$values = $_POST['values'];

foreach ($values as $dado) {
    $nomeAt = $dado['Nome'];
    $qtdAt = $dado['Qtd'];
    $id = $dado['id'];  

By the way, i strongly recommend using pdo, if not please make sure you validate your data before passing database query.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks friend ! Helped me a long way ! Btw, i still don't no how to use pdo, but i will validate the data ! Thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.