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:
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.
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.
– anuseranother