title: Import CSV/Excel data into MYSQL database and remove duplicate using codeigniter
body: <p>Today i came across a functionality where i need to i<strong>mport the CSV/Excel file in to MYSQL database via codeigniter.</strong> There is a special requirement from client where he can <strong>upload the CSV/Excel file</strong> in file upload field in <strong>HTML form</strong> and all data from CSV/Excel must <strong>import into MYSQL</strong> database table through PHP and remove the duplicates rows of data.But I faced some problems here.</p>
  
  <p><strong>My problems is:</strong></p>
  
  <ol>
  <li>when the user upload the csv file,its enter into the MYSQL database,but it have duplicates data of about 3-4 row.</li>
  <li>the data in csv file is about 8000 rows but the data insert in MYSQL db is only about 5000(included the duplicates data)</li>
  <li><p>this error occur when the file stop import the data</p>
  
  <p><a href="https://i.stack.imgur.com/REID5.png" rel="nofollow noreferrer">importcsv_error</a></p></li>
  </ol>
  
  <p>This is my model</p>
  
  <pre><code>&lt;?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'=&gt; $line[0] ,
                                      'product_description' =&gt; $line[1] ,
                                      'cust_name' =&gt; $line[2] ,
                                      'size' =&gt; $line[3] ,
                                      'colour' =&gt; $line[4],
                                      'process_description' =&gt; $line[5],
                                      'output' =&gt; $line[6],
                                      'material_part' =&gt; $line[7],
                                      'printingOutput' =&gt; $line[8]
                                      );
  
                      $q=$this-&gt;db-&gt;select('sindi_productprocess_temp', $prevQuery)
                              -&gt;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 -&gt; db-&gt;query($q);
  
                  if($prevResult-&gt;num_rows &gt; 0){
                      //update process data
  
                      $data = array(
                                  'articleno' =&gt; $line[0] ,
                                      'product_description' =&gt; $line[1] ,
                                      'cust_name' =&gt; $line[2] ,
                                      'size' =&gt; $line[3] ,
                                      'colour' =&gt; $line[4],
                                      'process_description' =&gt; $line[5],
                                      'output' =&gt; $line[6],
                                      'material_part' =&gt; $line[7],
                                      'printingOutput' =&gt; $line[8]
                                  );
  
  
                      $this-&gt;db-&gt;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-&gt;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-&gt;db-&gt;update('sindi_productprocess_temp');
  
  
                  }else{
                  for($i = 0, $j = count($line); $i &lt; $j; $i++){  
                        $data = array('articleno' =&gt; $line[0] ,
                                      'product_description' =&gt; $line[1] ,
                                      'cust_name' =&gt; $line[2] ,
                                      'size' =&gt; $line[3] ,
                                      'colour' =&gt; $line[4],
                                      'process_description' =&gt; $line[5],
                                      'output' =&gt; $line[6],
                                      'material_part' =&gt; $line[7],
                                      'printingOutput' =&gt; $line[8]
                                       );
  
              $data['crane_features']=$this-&gt;db-&gt;insert('sindi_productprocess_temp', $data);
                  }
                   $i++;
              }
  }
  fclose($fp) or die("can't close file");
  
  }
  }
  function get_car_features_info(){
       $get_cardetails=$this-&gt;db-&gt;query("select * from sindi_productprocess_temp");
        return $get_cardetails;
  }
  }
  ?&gt;
  </code></pre>
  
  <p>This is my controller</p>
  
  <pre><code>function processupload(){
          $this-&gt;load-&gt;model('upload_services');
          $data['result']=$this-&gt;upload_services-&gt;upload_sampledata_csv();
          $data['query']=$this-&gt; upload_services-&gt;get_car_features_info();
          $this-&gt;load-&gt;view(' Upload_csv ',$data);
      }
  </code></pre>
  
  <p>And this is my view</p>
  
  <pre><code>&lt;form action="{$path}pro/processupload" method="post" enctype="multipart/form-data" name="form1" id="form1"&gt; 
     &lt;table&gt;
         &lt;tr&gt;
             &lt;td&gt; Choose your file: &lt;/td&gt;
                &lt;td&gt;&lt;input type="file" class="form-control" name="userfile" id="userfile" align="center"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;div class="modal-footer"&gt;
                      &lt;td colspan="2" &gt;&lt;input type="submit" id="submit" name="submit" value="Import"&gt;&lt;/td&gt;
                    &lt;/div&gt;
               &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt; 
  </code></pre>
  
  <p></p>
  
  <p>Can anybody please help me on this??
  Thank you.    </p>