I have a table and each row has 6 inputs that should be saved in the database as a product.
The system has a button in HTML, which adds another line with 6 inputs, that is, by clicking on submit
I need to save an indeterminate number 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 it and it returned the arrays correctly, now the problem is that I can not 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 add function that will insert the products in the database, I gave print_r
to $compra
to test 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
)
)