R
The problem is that in your array the data comes from parts, then it's not that simple align them so that the query assumes each value of each column of each sub-array in a row.Apart from that, we are facing an uneven array because the usuario He's going to his air.Through this code you can organize the array to then dynamize the insertion. However, it would be useful for you to plan (if you can) otherwise organize the code in the source that sends those data, so that it sends them to you with a structure that is consistent with what you need.This is the code I propose. I'm not going to work to insert it into your context, I think you can do without much difficulty. It's a good idea to put it in a context of transactions, as seen in your attempt.I just leave you the basics, which is to treat $_POST, to organize the data and then assemble the preparation of the query and its binding with the data.I've put a literal query to prove it should work, if you incorporate it well.#Para sacar los datos del usuario aparte
$usuario=array();
#Quitamos los datos del usario para poder combinar los otros datos
foreach ($_POST as $k=>$v){
if (!is_array($v)){
unset ($_POST[$k]);
$usuario[$k]=$v;
}
}
#Aquí combinamos los datos de cada array particular
#para armar una fila completa de valores
$rows=array_map(function(){return func_get_args();},...array_values($_POST));
//echo implode(",",$usuario);
$types="sssssss";
foreach ($rows as $row){
#Combinamos los datos del usuario, con los de cada fila
$params=array_merge($usuario,$row);
#Sólo para probar la consulta... borrar
$forTest="INSERT INTO spesa (usuario, fecha, codice, articolo, quantita, stato, note)
VALUES (".implode(',',$params).")";
echo $forTest.PHP_EOL;
#Consulta preparada
$sql="INSERT INTO spesa (usuario, fecha, codice, articolo, quantita, stato, note)
VALUES (?,?,?,?,?,?,?)";
#Desde la versión 5.6+ PHP permite usar ...$params
#en versiones anteriores hay que recurrir a call_user_func
$stmt = $con->prepare($sql);
$stmt->bind_param($types, ...$params);
if(!$stmt->excute()){
#Sólo para debug, no dejes esta línea con mensajes internos
echo $con->error;
}
}
ObservationIf the column fecha It's the guy. DATE or DATETIME, you must convert the value of fecha in the $_POST to a format YYYY/MM/DD. That you can do in the part where you assign $usuarios and is made unset.The output of the test variable $forTest is this:INSERT INTO spesa (usuario, fecha, codice, articolo, quantita, stato, note)
VALUES (Valter Lafratta,16/03/2020,302,Cacio Molisano Bianco Stagionato Kg. 1,5,1,Confermata,primer producto)
INSERT INTO spesa (usuario, fecha, codice, articolo, quantita, stato, note)
VALUES (Valter Lafratta,16/03/2020,245,Cacio rettangolare gr 1500,2,Confermata,segundo producto)
INSERT INTO spesa (usuario, fecha, codice, articolo, quantita, stato, note)
VALUES (Valter Lafratta,16/03/2020,624,Fusilli Mediterranei 500 gr,3,Confermata,tercer producto)
INSERT INTO spesa (usuario, fecha, codice, articolo, quantita, stato, note)
VALUES (Valter Lafratta,16/03/2020,21,LATTE F.P.S. DA 1/2 LT PET,4,Confermata,cuarto producto)