我已經看過其他問題和答案。這就是我得到我正在使用的基本代碼。我修改它以滿足我的需求。基本上我正在用2張桌子。第一個表創建一條記錄,然後第二個表是csv文件的所有記錄,它將表1中創建的id複製到表2的job_id字段中。如何導入帶或不帶標題的csv並允許記錄具有空列字段?
不起作用的是if任何csv行都沒有完全填充所有列。代碼只是插入到數據庫中的所有行都被填充,當它碰到具有空列的第一行時,它將停止運行。下面的兩個版本都沒有工作。
如果csv沒有標題,這裏是版本一。
if ($_FILES[csv][size] > 0) {
include_once('db.php');
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
$job_name = $_POST['job_name'];
$file_name = $_FILES[csv][name];
$date_created = $_POST['date_created'];
$sql = "INSERT INTO jobs (job_name, file_name, date_created) VALUES ('$job_name','$file_name','$date_created')";
mysql_query($sql) or die(mysql_error());
$job_id = mysql_insert_id();
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO people (job_id, company, first, last, address1, address2, city, state, zip, phone, email, tracking_number, year, make, model, image, misc1, misc2, misc3, misc4, misc5, misc6, list) VALUES ('".$job_id."', '".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])."', '".addslashes($data[10])."', '".addslashes($data[11])."', '".addslashes($data[12])."', '".addslashes($data[13])."', '".addslashes($data[14])."', '".addslashes($data[15])."', '".addslashes($data[16])."', '".addslashes($data[17])."', '".addslashes($data[18])."', '".addslashes($data[19])."', '".addslashes($data[20])."', '".addslashes($data[21])."') ");
}
}
while ($data = fgetcsv($handle,1000,",","'"));
//redirect
header('Location: upload.php?success=1&job_name='.$job_name.'&file_name='.$file_name.'&date_created='.$date_created.'&job_id='.$job_id.'');
die;
}
這是第二版,如果csv在頂部有標題行。
if ($_FILES[csv][size] > 0) {
include_once('db.php');
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
// get headers
$columns = fgetcsv($handle,1000,",","'");
$column_list = implode(",",$columns);
$column_list = "job_id,".$column_list;
$job_name = $_POST['job_name'];
$file_name = $_FILES[csv][name];
$date_created = $_POST['date_created'];
$sql = "INSERT INTO jobs (job_name, file_name, date_created) VALUES ('$job_name','$file_name','$date_created')";
mysql_query($sql) or die(mysql_error());
$job_id = mysql_insert_id();
//loop through the csv file and insert into database
while ($data = fgetcsv($handle,1000,",","'")) {
if ($data[0]) {
for ($i=0;$i<count($columns);$i++){
$data[$i]="'".mysql_real_escape_string($data[$i])."'";
}
$values = implode(",",$data);
$values = "'".$job_id."',".$values;
mysql_query('INSERT INTO people ('.$column_list.') VALUES ('.$values.')');
}
}
//redirect
header('Location: upload.php?success=1&job_name='.$job_name.'&file_name='.$file_name.'&date_created='.$date_created.'&job_id='.$job_id.'&column_list='.$column_list.'&values='.$values.'');
die;
}
更新:
仍然沒有與答案的工作。
這裏是csv與標題行。只有標題行之後的第一行將數據庫還原到數據庫中。我需要標題行之後的所有行才能將其寫入數據庫。
company,first,last,address1,address2,city,state,zip,phone,email,tracking_number,year,make,model,image,misc1,misc2,misc3,misc4,misc5,misc6,list
abc corp,john,doe,123 whatever st,suite 2,whatevercity,whateverstate,33333,1234567890,[email protected],yes,2006,toyota,corolla,image.jpg,something,something,something,something,something,something,something
,Jim,Smith,address 1,,city 1,state 1,zip 1,,,,2007,honda,accord,,,,,,,,
,Joe,Tester,address 2,,city 2,state 2,zip 2,,,,2008,nissan,maxima,,,,,,,,
查看我更新的問題 – leoarce 2014-11-06 16:04:52
在'人'表中允許使用空值嗎? – mudasobwa 2014-11-06 16:11:50
你的意思是允許null?是的,它被設置爲允許null。 – leoarce 2014-11-06 16:13:40