Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!