I am writing a PHP script that imports CSV data from a myriad of customers. The data has 12 rows and 10 columns (see below). When I use phpMyAdmin's MySQL Import, there is NO problem (all 12 rows import fine!) However, when I try to import using the PHP script, it only imports the first line, or the last line. I've Googled many different ways to write the same script to no avail. The goal is for the user to be able to upload a CSV into the table automatically.
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data) {
mysql_query("INSERT INTO summary_td (customer_id, month, Income, Savings, Revolving_Expenses, Utilities, Services, Luxuries, Charities, month_id) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//redirect
header('Location: import.php?success=1'); die;
}
?>
CSV Data:
1,January,5000,899,899,899,899,899,899,1 1,February,NULL,NULL,NULL,NULL,NULL,NULL,NULL,2 1,March,NULL,NULL,NULL,NULL,NULL,NULL,NULL,3 1,April,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4 1,May,NULL,NULL,NULL,NULL,NULL,NULL,NULL,5 1,June,NULL,NULL,NULL,NULL,NULL,NULL,NULL,6 1,July,NULL,NULL,NULL,NULL,NULL,NULL,NULL,7 1,August,NULL,NULL,NULL,NULL,NULL,NULL,NULL,8 1,September,NULL,NULL,NULL,NULL,NULL,NULL,NULL,9 1,October,NULL,NULL,NULL,NULL,NULL,NULL,NULL,10 1,November,NULL,NULL,NULL,NULL,NULL,NULL,NULL,11 1,December,NULL,NULL,NULL,NULL,NULL,NULL,NULL,12