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

Asked

Viewed 228 times

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

3 answers

1

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

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

    }

0

I put the foreach, but the way I did is just 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;
    }
}

0


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

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

$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";
  • It worked! Thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.