CSV file does not record all data in the database

Asked

Viewed 25 times

1

Good evening, in my code when I send the CSV file It records only the last line of the file, when I use TRUNCATE TABLE. Without the truncate saves all lines, but the goal is to update the data by deleting them and inserting new ones. Can anyone help me? Follow code:

if(isset($_FILES["file"])) { if(isset($_FILES["file"])) { $filename = $_FILES["file"]["name"]; $arquivotipo = $_FILES["file"]["type"]; $arquivotmp_name = $_FILES["file"]["tmp_name"]; $archived = $_FILES["file"]["size"]; }

}


$arquivo = fopen($arquivotmp_name, "r");

while(($dados= fgetcsv($arquivo,1000, ";")) !== FALSE)
    {
        $chapa = $dados[0];
        $departamento = $dados[1];
        $setor = $dados[2];
        
        
        $query1 = "TRUNCATE TABLE tabela"; //apaga todos os dados da tabela
        
        $queryexec1 = mysqli_query($conn, $query1);
        
        $query2 = "INSERT INTO tabela (chapa, departamento, setor) VALUES ('$chapa', '$departamento', '$setor')";
        
       $queryexec2 =  mysqli_query($conn, $query2);
        
    }
    
    if($queryexec1 && $queryexec2 !== FALSE)
        {
            echo "Dados enviados com sucesso!";
        }
    else
        {
            echo "Erro";
        }
  • 2

    You shouldn’t give TRUNCATE out of of the loop?

  • Dude worked out! Thanks, I’m new to the programming so some things go unnoticed!! Thanks a lot!!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.