-1
 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[0]) { 
            mysql_query("INSERT INTO PowerFlex (customerCode, postCode,Name, Address1,Address2) VALUES 
                ( 
 '".addslashes($data[0])."', 
 '".addslashes($data[1])."', 
 '".addslashes($data[2])."', 
 '".addslashes($data[3])."', 
 '".addslashes($data[4])."', 
 '".addslashes($data[5])."', 

 etc

                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",",'"')); 
    // 
}

此代码将我的 CSV 上传到数据库中,但不会跳过 CSV 第一行中的标题(字段),也不会根据我的数据库表的标题检查标题。我怎么做 ?

4

4 回答 4

0
if ($_FILES[csv][size] > 0) { 

    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 
    $data = fgetcsv($handle,1000,",",'"') // this for skipping first line in your file
    $data = fgetcsv($handle,1000,",",'"') //
    //loop through the csv file and insert into database 
    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO PowerFlex (customerCode, postCode,Name, Address1,Address2) VALUES 
                ( 
 '".addslashes($data[0])."', 
 '".addslashes($data[1])."', 
 '".addslashes($data[2])."', 
 '".addslashes($data[3])."', 
 '".addslashes($data[4])."', 
 '".addslashes($data[5])."', 

 etc

                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",",'"')); 
    // 
}
于 2014-08-20T06:21:49.710 回答
0

您可以尝试以下方法:

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 
    for ($lines = 0; $data = fgetcsv($handle,1000,",",'"'); $lines++) {
        if ($lines == 0) continue;

        if ($data[0]) { 
            mysql_query("INSERT INTO PowerFlex (customerCode, postCode,Name, Address1,Address2) VALUES 
                ( 
 '".addslashes($data[0])."', 
 '".addslashes($data[1])."', 
 '".addslashes($data[2])."', 
 '".addslashes($data[3])."', 
 '".addslashes($data[4])."', 
 '".addslashes($data[5])."', 

 etc

                ) 
            "); 
        } 
    } 
    // 
}
于 2013-11-05T12:28:43.553 回答
0

您的代码无法区分第一行和其余部分。它还依赖于列在 CSV 中的特定顺序。

fgetcsv()在开头做一个单独的初始并构建一个$csv_fields数组,然后使用这个数组按名称将文件的字段与表的字段映射。有人担心 csv 中缺少列以及冗余列,但我将选择在下面的代码中忽略它们。我相信你会找到一种更合适的方式来处理这些事情。

if ($_FILES[csv][size] > 0) 
  { 
  // the table fields
  $tbl_fields = array('customerCode', 'postCode,Name', 'Address1', 'Address2');
  //get the csv file 
  $file = $_FILES[csv][tmp_name]; 
  $handle = fopen($file,"r"); 
  // get the first line into a fields map
  $csv_fields = fgetcsv($handle,1000,",",'"');
  // we will insert only common fields
  $tbl_fields = array_intersect($tbl_fields,$csv_fields);
  // if there's not at least one common field, don't go on
  if(count($tbl_fields)>0)
    {
    // we need the table's field names as keys (see below)
    $tbl_fields = array_flip($tbl_fields);
   // now let's go after the data
    while($data = fgetcsv($handle,1000,",",'"'))
      {
      $data=array_map('addslashes',$data); // apply addslashes() to all values 
      $data=array_combine($csv_fields,$data); // csv fields assoc (key=>value)
      $data=array_intersect_key($data,$tbl_fields); // discard redundant
      $tbl_fields_str=implode("`,`",array_keys($data));
      $tbl_vals_str=implode("','",array_values($data));
      $q="INSERT INTO `PowerFlex` (`$tbl_fields_str`) VALUES ('$tbl_vals_str')";
      mysql_query($q);
      }
    }
  else
    echo "There's no data I can use!";
  }
于 2013-11-05T13:36:31.580 回答
0
$data = fgets(...); // <= skip first line as header

$data = fgets(...); // <= read first data

//loop through the csv file and insert into database 
do { 
    if ($data[0]) { 
      ...
    } 
} while ($data = fgetcsv($handle,1000,",",'"')); 
于 2013-11-05T12:29:03.833 回答