Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
Cannot get fgetcsv file to work with MySQL database

I have tried to get this to fgetcsv function to output all rows of CSV file into my database. It seems quite standard functionality but it still doesn't work.

Here is my code I am trying to run:

<?php
$file = $_FILES['records_csv']['tmp_name'];
$handle = fopen($file,"r");

while(($csv = fgetcsv($handle,1000,",")) !== false) {
    $sql = "INSERT INTO records(artist, title, type, size, year, status)
    VALUES ('".$csv[0]."', '".$csv[1]."', '".$csv[2]."', '".$csv[3]."','".$csv[4]."','ACTIVE')"; 
}

if ($conn->query($sql) === TRUE) {
    echo "File uploaded successfully!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}     

When I do run it, it only seems to enter the second line of the file into the database? I don't know why this is happening??

My CSV file looks like this:

The Jam,All Around The World,Single,7 inch,1977
The Jam,Strange Town,Single,7 inch,1979

And my form I am uploading from looks like this:

<form method="post" class="AddCSV_form" enctype="multipart/form-data">
    <input type="file" name="records_csv">
    <input type="submit" name="csv_sub" value="Upload">
</form>

Thanks.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

JUst as JustOnUnderMillions spotted out, change code to this:

$file = $_FILES['records_csv']['tmp_name'];
                    $handle = fopen($file,"r");

                    while(($csv = fgetcsv($handle,1000,",")) !== false)
                    {
                        $sql = "INSERT INTO records
                         (artist, title, type, size, year, status)
                        VALUES ('".$csv[0]."', '".$csv[1]."', '".$csv[2]."',
                        '".$csv[3]."','".$csv[4]."','ACTIVE')"; 

if ($conn->query($sql) === TRUE) {
                        echo "File uploaded successfully!";
                    } else {
                        echo "Error: " . $sql . "<br>" . $conn->error;
                    } 
                    }
over 4 years ago · Santiago Trujillo Relatório

0

$file = $_FILES['records_csv']['tmp_name'];
$handle = fopen($file,"r");
$values = array();
while(($csv = fgetcsv($handle,1000,",")) !== false)
{
   $values[] = "('".$csv[0]."', '".$csv[1].
               "', '".$csv[2]."','".$csv[3]."','".$csv[4]."','ACTIVE')"; 
}
$sql = "INSERT INTO records (artist, title, type, size, year, status) VALUES "
       . implode(',',$values); 

if ($conn->query($sql) === TRUE) {
   echo "File(s) uploaded successfully!";
} else {
   echo "Error: " . $sql . "<br>" . $conn->error;
} 

have a nice ...

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda