【问题标题】:PHP CSV upload checking and skipping headers linePHP CSV 上传检查和跳过标题行
【发布时间】:2013-11-16 07:44:24
【问题描述】:
 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 第一行中的标题(字段),也不会根据我的数据库表的标题检查标题。 我该怎么做?

【问题讨论】:

  • 除此之外,你还必须更新你的一般编程技能,这样你就可以想出更多的东西,而不仅仅是“我该怎么做?”像这样——addslashes 是在这里使用的错误函数。请继续阅读 a) 如何以正确的方式防止 SQL 注入,以及 b) 为什么不再使用 mysql_ 函数。

标签: php mysql csv upload header


【解决方案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 
    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

                ) 
            "); 
        } 
    } 
    // 
}

【讨论】:

    【解决方案2】:
    $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,",",'"')); 
    

    【讨论】:

    • 嘿兄弟,我不太擅长PHP,你能解释一下你提到的前两件事吗?我在那里做什么?
    • 阅读do {之前的2行。 fgets(...) 表示fgetcsv($handle,1000,",",'"')
    • 对不起,我不清楚,因为你在前两行写了两次相同的内容。请你完全重写代码。
    【解决方案3】:

    您的代码无法区分第一行和其他行。它还依赖于 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!";
      }
    

    【讨论】:

      【解决方案4】:
      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,",",'"')); 
          // 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-07
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多