将CSV / Excel数据导入MYSQL数据库并使用codeigniter删除重复数据

时间:2017-10-17 02:13:17

标签: php mysql excel codeigniter csv

今天我遇到了一个功能,我需要通过codeigniter 将CSV / Excel文件导入MYSQL数据库。客户有一个特殊要求,他可以上传CSV HTML表格中的文件上传字段中的/ Excel文件,CSV / Excel中的所有数据必须通过PHP导入MYSQL 数据库表并删除重复的数据行但我在这里遇到了一些问题。

我的问题是:

  1. 当用户上传csv文件时,它进入MYSQL数据库,但它有大约3-4行的重复数据。
  2. csv文件中的数据大约是8000行,但MYSQL db中的数据插入只有大约5000(包括重复数据)
  3. 文件停止导入数据时发生此错误

    importcsv_error

  4. 这是我的模特

    <?php
    class Upload_services extends CI_Model{
       function __construct(){
            parent::__construct();
      }
       function upload_sampledata_csv(){
            if(isset($_POST['submit'])){
                  $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
                    while(($line = fgetcsv($fp)) !== FALSE){
                        //check whether there are duplicate rows of data in database
                        $prevQuery = array(
                                        'articleno'=> $line[0] ,
                                        'product_description' => $line[1] ,
                                        'cust_name' => $line[2] ,
                                        'size' => $line[3] ,
                                        'colour' => $line[4],
                                        'process_description' => $line[5],
                                        'output' => $line[6],
                                        'material_part' => $line[7],
                                        'printingOutput' => $line[8]
                                        );
    
                        $q=$this->db->select('sindi_productprocess_temp', $prevQuery)
                                ->where('articleno',$line[0],
                                        'product_description', $line[1] ,
                                        'cust_name' , $line[2] ,
                                        'size', $line[3] ,
                                        'colour' , $line[4],
                                        'process_description' , $line[5],
                                        'output', $line[6],
                                        'material_part', $line[7],
                                        'printingOutput', $line[8]);
    
                    $prevResult = $this -> db->query($q);
    
                    if($prevResult->num_rows > 0){
                        //update process data
    
                        $data = array(
                                    'articleno' => $line[0] ,
                                        'product_description' => $line[1] ,
                                        'cust_name' => $line[2] ,
                                        'size' => $line[3] ,
                                        'colour' => $line[4],
                                        'process_description' => $line[5],
                                        'output' => $line[6],
                                        'material_part' => $line[7],
                                        'printingOutput' => $line[8]
                                    );
    
    
                        $this->db->set
                        (
                                        'articleno',$line[0],
                                        'product_description', $line[1] ,
                                        'cust_name' , $line[2] ,
                                        'size', $line[3] ,
                                        'colour' , $line[4],
                                        'process_description' , $line[5],
                                        'output', $line[6],
                                        'material_part', $line[7],
                                        'printingOutput', $line[8]
                        );
    
                        $this->db-where('articleno',$line[0],
                                        'product_description', $line[1] ,
                                        'cust_name' , $line[2] ,
                                        'size', $line[3] ,
                                        'colour' , $line[4],
                                        'process_description' , $line[5],
                                        'output', $line[6],
                                        'material_part', $line[7],
                                        'printingOutput', $line[8]
                                         ); 
    
                        $this->db->update('sindi_productprocess_temp');
    
    
                    }else{
                    for($i = 0, $j = count($line); $i < $j; $i++){  
                          $data = array('articleno' => $line[0] ,
                                        'product_description' => $line[1] ,
                                        'cust_name' => $line[2] ,
                                        'size' => $line[3] ,
                                        'colour' => $line[4],
                                        'process_description' => $line[5],
                                        'output' => $line[6],
                                        'material_part' => $line[7],
                                        'printingOutput' => $line[8]
                                         );
    
                $data['crane_features']=$this->db->insert('sindi_productprocess_temp', $data);
                    }
                     $i++;
                }
    }
    fclose($fp) or die("can't close file");
    
    }
    }
    function get_car_features_info(){
         $get_cardetails=$this->db->query("select * from sindi_productprocess_temp");
          return $get_cardetails;
    }
    }
    ?>
    

    这是我的控制器

    function processupload(){
            $this->load->model('upload_services');
            $data['result']=$this->upload_services->upload_sampledata_csv();
            $data['query']=$this-> upload_services->get_car_features_info();
            $this->load->view(' Upload_csv ',$data);
        }
    

    这是我的观点

    <form action="{$path}pro/processupload" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
       <table>
           <tr>
               <td> Choose your file: </td>
                  <td><input type="file" class="form-control" name="userfile" id="userfile" align="center"/></td>
                  <td><div class="modal-footer">
                        <td colspan="2" ><input type="submit" id="submit" name="submit" value="Import"></td>
                      </div>
                 </td>
          </tr>
      </table> 
    

    有人可以帮我吗? 谢谢。

2 个答案:

答案 0 :(得分:1)

有三个错误:

  1. 你正在将多个参数传递给WHERE,你传递的错误
  2. 当我们使用SELECT和WHERE时,使用get()而不是query(),我们在编写完整的自定义查询时使用查询。
  3. 您在其他情况下使用for($i = 0, $j = count($line); $i < $j; $i++)添加重复记录
  4. 我还添加了两条评论:

    1. 从CSV中删除标题(如在代码循环中,第一次迭代仅检查标题)
    2. 最好使用关联数组
    3. 而不是索引数组

      以下是您的模型的完整代码:

          <?php
      class Upload_services extends CI_Model
      {
          function __construct()
          {
              parent::__construct();
          }
          function upload_sampledata_csv()
          {
      
              if(isset($_POST['submit'])){
                  $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
      
                  //fgetcsv($fp, 1000, ",");// to remove header from CSV
      
                  /*** If you want associated Array *****/
      
                  /*
                  $csv = array_map("str_getcsv", file($_FILES['userfile']['tmp_name'],FILE_SKIP_EMPTY_LINES));
                  $keys = array_shift($csv);
                  //To turn all the rows into a nice associative array you could then apply:
      
                  foreach ($csv as $i=>$row) {
                      $csv[$i] = array_combine($keys, $row);
                  }
                  */
      
      
                  while(($line = fgetcsv($fp)) !== FALSE)
                  {
      
      
                      //check whether there are duplicate rows of data in database
                      $prevQuery = array(
                          'articleno'=> $line[0] ,
                          'product_description' => $line[1] ,
                          'cust_name' => $line[2] ,
                          'size' => $line[3] ,
                          'colour' => $line[4],
                          'process_description' => $line[5],
                          'output' => $line[6],
                          'material_part' => $line[7],
                          'printingOutput' => $line[8]
      
                      );
                      $where = array('articleno' => $line[0],
                                    'product_description' => $line[1] ,
                                    'cust_name'  => $line[2] ,
                                    'size' => $line[3] ,
                                    'colour'  => $line[4],
                                    'process_description'  => $line[5],
                                    'output' => $line[6],
                                    'material_part' => $line[7],
                                    'printingOutput' => $line[8]);
      
      
                      $q = $this->db->select("*")
                                  ->where($where)
                                  ->from('sindi_productprocess_temp');
      
      
      
                      $prevResult = $this->db->get();
      
      
      
                      if($prevResult->num_rows > 0){
                          //update process data
      
                          $data = array(
                              'articleno' => $line[0] ,
                              'product_description' => $line[1] ,
                              'cust_name' => $line[2] ,
                              'size' => $line[3] ,
                              'colour' => $line[4],
                              'process_description' => $line[5],
                              'output' => $line[6],
                              'material_part' => $line[7],
                              'printingOutput' => $line[8]
      
                          );
      
      
                          $this->db->set
                          (
                              'articleno',$line[0],
                              'product_description', $line[1] ,
                              'cust_name' , $line[2] ,
                              'size', $line[3] ,
                              'colour' , $line[4],
                              'process_description' , $line[5],
                              'output', $line[6],
                              'material_part', $line[7],
                              'printingOutput', $line[8]
                          );
      
                          $this->db-where
                          (
                              'articleno',$line[0],
                              'product_description', $line[1] ,
                              'cust_name' , $line[2] ,
                              'size', $line[3] ,
                              'colour' , $line[4],
                              'process_description' , $line[5],
                              'output', $line[6],
                              'material_part', $line[7],
                              'printingOutput', $line[8]
                          );
      
                          $this->db->update('sindi_productprocess_temp');
      
      
                      }else{
      
                              $data = array(
                                  'articleno' => $line[0] ,
                                  'product_description' => $line[1] ,
                                  'cust_name' => $line[2] ,
                                  'size' => $line[3] ,
                                  'colour' => $line[4],
                                  'process_description' => $line[5],
                                  'output' => $line[6],
                                  'material_part' => $line[7],
                                  'printingOutput' => $line[8]
      
                              );
      
                              $data['crane_features']=$this->db->insert('sindi_productprocess_temp', $data);
      
                      }
                  }
                  fclose($fp) or die("can't close file");
      
      
              }
          }
          function get_car_features_info()
          {
              $get_cardetails=$this->db->query("select * from sindi_productprocess_temp");
              return $get_cardetails;
          }
      }
      ?>
      

答案 1 :(得分:0)

尝试此代码:-

Excel/CSV File

控制器文件功能

function import_flat(){
    if(isset($_POST['submit'])){
        $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
        while(($line = fgetcsv($fp)) !== FALSE){
        $query = $this->db->get_where('flat', array('name' => $line[0] ,'wingid' => $line[1]));
        if($query->num_rows() > 0){
            //update process data
            $data = array('name' => $line[0] ,'wingid' => $line[1]);
            $this->db->where('wingid', $line[1]);
            $this->db->where('name', $line[0]);
            $this->db->update('flat', $data);
            echo "Data update";
        }
        else
        {
            for($i = 1; $i < count($line); $i++)
            {  
                $data = array('name' => $line[0] ,'wingid' => $line[1]);
                print_r($data);
                print_r(count($line));
                $data['flats']=$this->db->insert('flat', $data);
            }
            $i++;
        }       
    }
    fclose($fp) or die("can't close file");
    }
}

查看文件

<?php echo form_open_multipart('Flat/flats/importflat', array('method' => 'post')); ?>
      <div class="form-group">
          <label>Excel File For Flat</label>
          <br>
          <span class="btn btn-primary fileinput-button">
               <i class="fa fa-plus"></i>
          <span>Add file</span>
          <input class="form-control" type="file" name="userfile" id="userfile" required>
          </span>
       </div>
       <button type="submit" class="mb-sm btn btn-primary" id="submit" name="submit">Submit</button>
<?php echo form_close(); ?>

Database table for flat