如何导入带或不带标题的csv并允许记录有空列字段?

时间:2014-11-06 15:41:44

标签: php mysql forms csv

我已经查看了其他问题和答案。这就是我如何获得我正在使用的基本代码。我根据自己的需要修改了它。基本上我在使用2张桌子。第一个表创建一个记录,然后第二个表是csv文件的所有记录,它将表1中创建的id复制到表2的job_id字段中。

不起作用的是,如果任何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@email.com,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,,,,,,,,

1 个答案:

答案 0 :(得分:1)

您尝试在“截断”数据上访问不存在的数组键。看:

$data = fgetcsv($handle,1000,",","'")

这里构建了正好N个元素的数组,其中N是从CSV获得的值的计数。然后你打电话:

$data[$i]="'".mysql_real_escape_string($data[$i])."'";
<{1}}范围内$i

。但[0..columns-1]数组中的项目可能较少。以下是一个热门修复:

data

UPD $data[$i]="'".mysql_real_escape_string( ( array_key_exists( $i, $data ) ? $data[$i] : '' ) )."'"; 会阻止操作公司名称为空的行,因为if( $data[0])的评估结果为if ('')