I'm pulling a CSV file into a script, parsing it and then updating my MySQL table. The problem is that it's only inserting the data from the second row of the CSV file (first row is being skipped because it's the headers of the columns). Here's my code:
$f = fopen("sample.csv", "r");
while(($line = fgetcsv($f)) !== FALSE){
$time = $line[0];
//SKIP FIRST LINE (WITH HEADERS)
if($time == "Time"){
continue;
}
else{
$size = $line[1];
$location = $line[2];
$county = $line[3];
$state = $line[4];
$lat = $line[5];
$lon = $line[6];
$comments = $line[7];
$sql = "INSERT INTO sample (time, size, location, county, state, lat, lon, comments) VALUES ('$time', '$size', '$location', '$county', '$state', '$lat', '$lon', '$comments')";
$result = $mysqli->query($sql);
//CHECK TO SEE IF THE LOOP IS WORKING THROUGH ALL DATA
echo $size;
}
}
fclose($f);
The issue is that it skips the first row successfully and then on the second loop through, it successfully updates the database. As you can see, I've echo'ed out the value of $size and it echoes all the values of all the rows meaning it's looping through all the data correctly. I'm not sure why it's not adding that data into each subsequent row.
Thanks for your help!