How to insert multiple arrays at the same time in the database?

1

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
        )
)
    
asked by anonymous 16.11.2018 / 17:36

3 answers

0
  

To do this, provide a list of values in parentheses for each record and separate the lists with commas.

Use the foreach to generate the variable $values which will serve VALUE of the INSERT statement

$compra = array_map(null,$descricao,$tamanho,$cor,$qtde,$pagamento,$valor);

$i=0;
foreach($compra AS $indice => $val) {
  $values .= " ('$descricao[$i]', '$tamanho[$i]', '$cor[$i]', '$qtde[$i]', '$pagamento[$i]', '$valor[$i]' ),";
  $i++;
}
//RETIRA A ULTIMA VIRGULA
$values=substr($values, 0, -1);

echo "INSERT ..... VALUES $values";
    
16.11.2018 / 19:52
1

The insertion has to be done one at a time.    Make a

foreach($compra as $compras){
    //comando que adicionará no banco

    }
    
16.11.2018 / 18:03
0

I put the foreach, but the way I did it is only saving the first array.

function adicionar($compra){

        date_default_timezone_set('America/Sao_Paulo');
        $data = date('Y-m-d H:i');

        foreach($compra as $compras){                   
            $sql = "INSERT INTO 'vendas' ('descricao', 'tamanho', 'cor','qtde','pagamento','valor', 'data') VALUES (:descricao, :tamanho, :cor, :qtde, :pagamento, :valor, :data)";

            $sql = $this->pdo->prepare($sql);
            $sql->bindValue(':descricao', $compras[0]);
            $sql->bindValue(':tamanho', $compras[1]);
            $sql->bindValue(':cor', $compras[2]);
            $sql->bindValue(':qtde', $compras[3]);
            $sql->bindValue(':pagamento', $compras[4]);
            $sql->bindValue(':valor', $compras[5]);
            $sql->bindValue(':data', $data);
            $sql->execute();

            return true;
    }
}
    
16.11.2018 / 19:38