While there are similar questions answered, I could not solve my problem. I have generated from Php, a two-dimensional array obtained from a previous page, from where I passed the data by the POST method, now I need to save them in a table previously created in MySQL. So far I did the following:
<?php
include ("datos.php");
include ("funciones.php");
$tabla=array ();
$i=0;
$j=0;
foreach ($_POST as $fila) {//guardo el POST en mi arreglo
if($j<=9) {
$tabla[$i][$j]=$fila;
if($j==9) {
$j=0;
$i++;
} else
$j++;
}
}
for($i=0;$i<contarFilas($tabla);$i++) {
//Muestro lo que guardé en el arreglo,verifiqué que está OK. contarFilas es una función que hace justamente "eso", no viene al caso.
for($j=0;$j<=9;$j++) {
echo '<br><b>'.$tabla[$i][$j].'</b><br>';
}
}
print_r($tabla); //para ver la estructura del arreglo.
Now I wanted to save them in a MySQL table in the following way:
for($f=0;$f<contarFilas($tabla);$f++) {
$pasar="INSERT INTO cambios(pyp,aoi,posicion,cod_prog,cod_lista,obs_pyp,obs_aoi,agente,fecha,hora) values ('$tabla[$f][0]' ,'$tabla[$f][1]' ,'$tabla[$f][2]','$tabla[$f][3]' ,'$tabla[$f][4]','$tabla[$f][5]','$tabla[$f][6]','$tabla[$f][7]','$tabla[$f][8]','$tabla[$f][9]' )";}
if ($conn->query($pasar) === TRUE) {
echo "Se pasaron los datos";
} else {
echo "Hubo un error al pasar los datos : " . $conn->error;
}
Obviously you can not, because after investigating (as far as I could go), I understood that an array can not be saved directly to a MySQL table without converting it into a string. Using for example the implode function. if I do for example:
$cadena=implode("",$tabla);
echo $cadena;
returns an error, but instead:
$cadena=implode("",$tabla[$x]);//o cualquier índice.
echo $cadena;
returns the row "x" of my table, with the separation that is passed as the first argument. But it does not admit the following way:
$cadena=implode("",$tabla[$x][$y]);
Then. How could I save the elements of my $ table array in one table (worth the redundancy) of MySQL one by one? I hope I have been clear, and sorry for the rusticity of the code. I am practically new in this discipline. Greetings. And from now on, Thank you.
I add how the obtained arrangement is and what I want to save.
Array ( [0] => Array ( [0] => MAITV00400 [1] => EAX67187104 [2] => 0CK104CK56A [3] => EAE61081701 [4] => C1100 [5] => [6] => [7] => Vega,Fernando_Maximiliano [8] => 6/11/2018 [9] => 7:47 )
[1] => Array ( [0] => MAITV00400 [1] => EAX67187104 [2] => 0CK104CK56A [3] => 0CC103BK5BA [4] => C1102 [5] => [6] => [7] => Vega,Fernando_Maximiliano [8] => 6/11/2018 [9] => 7:47 )
[2] => Array ( [0] => MAITV00400 [1] => EAX67187104 [2] => EAE61081701 [3] => 0CH5220K618 [4] => C1104 [5] => [6] => [7] => Vega,Fernando_Maximiliano [8] => 6/11/2018 [9] => 7:47 )
[3] => Array ( [0] => MAITV00400 [1] => EAX67187104 [2] => EAN64108201 [3] => EAN64430501 [4] => IC401 [5] => [6] => [7] => Vega,Fernando_Maximiliano [8] => 6/11/2018 [9] => 7:47 )
[4] => Array ( [0] => MAITV00400 [1] => EAX67187104 [2] => EAN64108201 [3] => EAN64430501 [4] => IC402 [5] => [6] => [7] => Vega,Fernando_Maximiliano [8] => 6/11/2018 [9] => 7:47 ) )