Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda