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.