Foreach running only once

1

I have a code that reads a txt and writes to the database the txt data, but the foreach does not do the replay and writes only the first line.

$arquivo_tmp = $_FILES['arquivo']['tmp_name'];

$dados = file($arquivo_tmp);

foreach($dados as $linha){
    $linha = trim($linha);
    $valor = explode('|', $linha);
    var_dump($valor);

$COD = $valor[1];
$NOME = $valor[2];
$CARACT = $valor[3];
$END = $valor[4];
$CPF = $valor[5];

$result_usuario = "INSERT INTO info (COD, NOME, CARACT, END, CPF) VALUES ('$COD','$NOME','$CARACT','$END','$CPF')";

$resultado_usuario = mysqli_query($conn, $result_usuario);

}

My txt looks like this:

|42|Carlos Paiva da Silva|2|Av. Alameda Santos, 255|15265482536|

(there is not only one line in the file, just an example)

    
asked by anonymous 11.09.2018 / 14:00

2 answers

1

By indentation your code should end after var_dump :

foreach($dados as $linha){
    $linha = trim($linha);
    $valor = explode('|', $linha);
    var_dump($valor);

But it does not have } ; with this, (possibly) the block is closed in what would be the end of the method. Add the close key :

foreach($dados as $linha){
    $linha = trim($linha);
    $valor = explode('|', $linha);
    var_dump($valor);
}

edited after comments

The variable $valores (after explode ) contains your actual list of items to insert. I changed the name of the variable to be more intuitive, see if it solves:

$arquivo_tmp = $_FILES['arquivo']['tmp_name'];

$dados = file($arquivo_tmp);

foreach($dados as $linha){
    $linha = trim($linha);
    $valores = explode('|', $linha);
    var_dump($valores);

    foreach($valores as $valor){

        $COD = $valor[1];
        $NOME = $valor[2];
        $CARACT = $valor[3];
        $END = $valor[4];
        $CPF = $valor[5];

        $result_usuario = "INSERT INTO info (COD, NOME, CARACT, END, CPF) VALUES ('$COD','$NOME','$CARACT','$END','$CPF')";

        $resultado_usuario = mysqli_query($conn, $result_usuario);
    }
}

An alternative would be to create a string concatenating the values and give only an insert , since the first one works (yes, this is gambiarra):

$arquivo_tmp = $_FILES['arquivo']['tmp_name'];

$dados = file($arquivo_tmp);
$result_usuario = "";

foreach($dados as $linha){
    $linha = trim($linha);
    $valores = explode('|', $linha);
    var_dump($valores);

    $result_usuario = "INSERT INTO info (COD, NOME, CARACT, END, CPF) VALUES";
    foreach($valores as $valor){    
        $COD = $valor[1];
        $NOME = $valor[2];
        $CARACT = $valor[3];
        $END = $valor[4];
        $CPF = $valor[5];

        $result_usuario .= "('$COD','$NOME','$CARACT','$END','$CPF'),";
    }

    $result_usuario = substr($result_usuario, 0, -1); 

    $resultado_usuario = mysqli_query($conn, $result_usuario);
}
    
11.09.2018 / 14:26
4
//Recebe os dados do formulario
$arquivo_tmp = $_FILES['arquivo']['tmp_name'];

//ler todo o arquivo para um array
$dados = file($arquivo_tmp);

foreach($dados as $linha){
    $linha = trim($linha);
    $valor = explode('|', $linha);

   $COD = $valor[1];
   $NOME = $valor[2];
   $CARACT = $valor[3];
   $END = $valor[4];
   $CPF = $valor[5];

   //prepara os values
   $result_usuario .=  "('$COD','$NOME','$CARACT','$END','$CPF'),";

}

//retira a ultima virgula
$values=substr($result_usuario, 0, -1);

//a query para inserir os dados no banco

    //$conn = ......

    $result_usuario = "INSERT INTO info (COD, NOME, CARACT, END, CPF) VALUES $values";

    $resultado_usuario = mysqli_query($conn, $result_usuario);

Bank table

txtfile

AfterrunningPHPcode

  

AsingleINSERT...VALUESstatementcanaddmultiplerecordstoatableifyousupplymultiplelistsofvalues.Todothis,providealistofvaluesinparenthesesforeachrecordandseparatethelistswithcommas.

Forexample:

"INSERT INTO info (COD, NOME, CARACT, END, CPF) VALUES
('42','Carlos Paiva da Silva 2','2','Av. Alameda Santos, 2','5265482532'),('43','Carlos Paiva da Silva 3','3','Av. Alameda Santos, 3','5265482533'), etc...

The declaration shown creates records in the info table, assigning the listed values to the COD, NOME, CARACT, END, CPF columns of each record. The id (if any) column is not listed explicitly, so MySQL assigns a string value to that column in each record.

A multi-line INSERT statement is logically equivalent to a set of individual single-line declarations. However, multi-line declaration is more efficient because the server can process all lines at once rather than in separate operations. When you have many records to add, multi-line declarations provide better performance and reduce server load.

    
11.09.2018 / 15:47