I have the following code:
foreach ( $_POST["num_cod"] as $num_cod) {
echo $num_cod;
}
foreach ( $_POST["fec_cod"] as $fec_cod ) {
echo $fec_cod;
}
foreach ( $_POST["nro_oper"] as $nro_oper ) {
echo $nro_oper;
}
Where the variables: $num_cod
, $fec_cod
and $nro_oper
, are arrays with n
elements each where the first element of the first array corresponds to the first element of the second and the first element of the third array Each column corresponds to a column in a table. What I need is that each element of each array in its corresponding column be loaded in the table of mention and that the relationship between the data be maintained. I have tried several ways but I have not obtained the expected results, this is:
- Making a
INSERT
for eachforeach
: all the variables are inserted but there is no data correlativity, that is, the elements of the first array are loaded inn
rows, those of the second array in the followingn
rows, etc., filling the other fields with zeros. - Nesting the
foreach
and a singleINSERT
for the three variables:3.n
repetitions of the data are loaded. - Making
INSERT
out offoreach
does not work either.
If someone can help me since the php.net
documentation has not helped me too much. With array_merge()
I could make a matrix with the three arrays but I would not know how to take them to the mysql
table. The arrays always complicate me. I am very grateful for the time that the members of this forum use to help those we know less.
With:
echo $_POST['fec_cod'];
echo $_POST['nro_cod'];
echo $_POST['nro_oper'];
With:
if ( !empty($_POST["nro_cod"]) && is_array($_POST["nro_cod"]) ) {
echo "<ul>";
foreach ( $_POST["nro_cod"] as $nro_cod ) {
echo "<li>";
echo "Código: ",$nro_cod;
echo "</li>";
}
echo "</ul>";
and it also works with the other two variables.