Work with three arrays and insert with PHP in MySQL database

0

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 each foreach : all the variables are inserted but there is no data correlativity, that is, the elements of the first array are loaded in n rows, those of the second array in the following n rows, etc., filling the other fields with zeros.
  • Nesting the foreach and a single INSERT for the three variables: 3.n repetitions of the data are loaded.
  • Making INSERT out of foreach 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.

    
asked by DieWalever 20.09.2018 в 16:50
source

2 answers

0

generate sql from array

I found the answer. Thanks to @Alberto Siurob, who was the ONLY one who cared about providing help.

    
answered by 21.09.2018 / 17:00
source
2

Easy, first, are you sure that when $_POST arrives it is an array? Usually a delimited string is sent and you apply a explode to pass it to an array. Now if the 3 data are related in your index how you say, you only need to cycle them but with a for , not with foreach

$num_cod = $_POST['num_cod'];
$fec_cod = $_POST['fec_cod'];
$nro_oper = $_POST['nro_oper'];

for( $i = 0; $i < count( $num_cod ); $i++ ){
    $cod = $num_cod[$i];
    $fec = $fec_cod[$i];
    $nro = $nro_oper[$i];
    //Ya puedes hacer tu query, sería masomenos así
    $query = "INSERT INTO TABLA(CODIGO, FECHA, NUMERO) VALUES($cod, $fec, $nro)";
}

As all the indexes are related (In case we have a strong front-end) you would not already have problems in cycling and get the data from the same index.

Greetings

    
answered by 20.09.2018 в 16:59