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.