1
I have a table and each row has 6 inputs that must be saved in the database as a product.
The system has a button in HTML, which adds another line with 6 inputs, that is, when clicking on submit
I need to save an indeterminate amount of forms at once to the database.
<div class="row">
<div class="celula">
<input type="text" name="descricao[]" required>
</div>
<div class="celula">
<input type="text" name="tamanho[]" required>
</div>
<div class="celula">
<input type="text" name="cor[]" required>
</div>
<div class="celula">
<input type="number" name="qtde[]" required>
</div>
<div class="celula">
<input type="text" name="pagamento[]" required>
</div>
<div class="celula">
<input type="number" name="valor[]" required>
</div>
</div>
I used array_map
to separate the values the way I need and he returned the arrays correctly, now the problem is that I can’t save all the arrays, always save only the first.
if(isset($_POST['descricao']) && !empty($_POST['descricao'])) {
$descricao = $_POST['descricao']; //array
$tamanho = $_POST['tamanho']; //array
$cor = $_POST['cor']; //array
$qtde = $_POST['qtde']; //array
$pagamento = $_POST['pagamento']; // array - forma de pagamento
$valor = $_POST['valor']; //array
$compra = array_map(null,$descricao,$tamanho,$cor,$qtde,$pagamento,$valor);
$adicionar = $venda->adicionar($compra);
print_r($compra);
}
I sent everything to the function add that will insert the products in the bank, I gave a print_r
in $compra
to make a test with products 'w' and 'Q' I wanted to give
I need to know how to save them:
Array(
[0] => Array
(
[0] => WWWWWWWWWWWWWWWWWWWWWWWWW
[1] => WWWWWWWWWWWW
[2] => WWWWWWW
[3] => 10
[4] => WWWW
[5] => 100
)
[1] => Array
(
[0] => QQQQQQQQQQQQQQQQQQQQQQQQQQQ //descricao
[1] => QQQQQQQQQQQQQQ //tamanho
[2] => QQQQQQQ //cor
[3] => 20 //quantidade
[4] => QQQQQQQQ //forma de pagamento
[5] => 200 //valor
)
)
If any response solves your difficulty, consider marking the answer as accepted, see how and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252