Insert data from an array to mysql with php

0

I have two tables related to the principal and the detail. I have to register for example:

 Tabla 1  
 Idpersona persona  
  01       Juan

Tabla 2

ID idpersona pedido
1     01     zcahs
2     01     bitcoin
3     01     Eterium

To insert data into the second table to the inputs I define as fixes

<input name="pedido []" value="zcahs, bitcoin, eterium"/>

In which the value comes separated by commas (,), how to decipher that values so that they are inserted each one in a row in the database.

Thanks for the help.

    
asked by Capzzula 25.02.2018 в 14:33
source

1 answer

1

It would be easier if it is done like this:

form.php

              save

save.php

<?php
$nombre = $_GET['nombre'];
$pedido = $_GET['pedido'];

//Utilizando PDO.

$dbh = new PDO('mysql:host=localhost;dbname=test', 'username', 'password'); 
$stmt = $dbh->prepare("INSERT INTO Tabla1 (persona) VALUES(?)"); 
$dbh->beginTransaction(); 
$tmt->execute(array($nombre)); 
$dbh->commit(); 
$lastId = $dbh->lastInsertId();
$parts = explode(",",$pedido);
foreach($parts as $part){
$stmt = $dbh->prepare("INSERT INTO Tabla2 (idpersona, pedido) VALUES(?,?)"); 
$dbh->beginTransaction(); 
$tmt->execute(array($lastId,$part)); 
$dbh->commit();
}

?>

I hope it works for you, regards

    
answered by 25.02.2018 / 17:53
source