Insert multiple with PDO php and MySql

2

My situation,

A POST form with a text input and two input radios.

Inputs are named respectively nome[0] and tipo[0] .

When someone adds more fields to the form, I put .attr to get nome[1] and tipo[1] and successively as it adds more inputs.

  • I did this to have how to transfer and prepare their information via POST in my php upload file.

  • It also helped me to keep radio buttons in the same group on different lines.

In the end, I would like these records in the MySql table in a table, in the name and type spaces.

So PHP

<?php

include 'conexao.php';

$nome = $_POST['nome'];  // Aqui pega input text, o valor do name="nome[0]".

$tipo = $_POST['tipo'];  // Aqui pega do input radio, o valor do name="tipo[0]".


  if (is_array($nome)){
    if (is_array($tipo)){
       
       foreach($nome as $valornome) {
         foreach($tipo as $valortipo) {
        
$carimbo = $con->prepare("INSERT INTO ingressos (nome,tipo) VALUES (?,?)");

$carimbo->bindValue(1,$valornome,PDO::PARAM_STR);
$carimbo->bindValue(2,$valortipo,PDO::PARAM_STR);

$carimbo->execute();
 

}}}}

It went wrong, but ...!

I can store only the correct radios, the name is cloned by the number of inputs generated :(

Form

HereMySql

I've been seeing some in materials before sending the data to have to organize the information I get from the vectors and then send, but I do not quite understand the texts I had access to ...

In short, what is the way to situations like this? Imagining, for example, a third input option ... I would love to get input on topics and readings.

My knowledge is very artificial, I understand a little of what is happening but not the depth of the logic of the codes.

Thank you in advance!

    
asked by anonymous 29.01.2018 / 02:27

2 answers

2

The problem is that you are running INSERT twice as much, this is because the first foreach will traverse all (2 inputs = 2 insert ) names and then go (2 inputs = +2 insert ) .

29.01.2018 / 11:41
1

With these threaded foreachs what happens is for element in $nome the second foreach is executed and only then returns the first which basically combines all values. For example if each array has 4 elements 16 inserts will be generated when they should be only 4.

Combination example - ideone

To fix this situation the first step is to combine the two arrays with array_map() where each element of $tipos and $nomes will be paired together in a new array.

With the array mounted correctly you can now pass it directly to execute() .

$nomes = !empty($_POST['nome']) && is_array($_POST['nome']) ?  $_POST['nome'] : array();
$tipos = !empty($_POST['tipo']) && is_array($_POST['tipo']) ? $_POST['tipo'] : array(); 

$novo = array_map(function($nome, $tipo){ return array($nome, $tipo);}, $nomes, $tipos);

foreach($novo as $item){       
    $carimbo = $con->prepare("INSERT INTO ingressos (nome,tipo) VALUES (?,?)");
    if(!$carimbo->execute($item)){
        print_r($carimbo->errorInfo());
    }
}   

Or alternatively:

$ingressos['nomesOK'] = !empty($_POST['nome']) && is_array($_POST['nome']);
$ingressos['tiposOK'] = !empty($_POST['tipo']) && is_array($_POST['tipo']);


if($ingressos['nomesOK'] && $ingressos['tiposOK']){
    $novo = array_map(function($nome, $tipo){ return array($nome, $tipo); }, $_POST['nome'], $_POST['tipo']);
    foreach($novo as $item){       
        $carimbo = $con->prepare("INSERT INTO ingressos (nome,tipo) VALUES (?,?)");
        if(!$carimbo->execute($item)){
            print_r($carimbo->errorInfo());
        }
    } 
}   
$nomes = array('fulano', 'joão', 'maria', 'george');
$tipos = array ('meia', 'inteira', 'meia', 'meia');

Given the following entries, array_map() converts them to the format:

array_map example - ideone

Array
(
    [0] => Array
        (
            [0] => fulano
            [1] => meia
        )

    [1] => Array
        (
            [0] => joão
            [1] => inteira
        )

    [2] => Array
        (
            [0] => maria
            [1] => meia
        )

    [3] => Array
        (
            [0] => george
            [1] => meia
        )

)
    
29.01.2018 / 11:32