如何更新从CSV文件导入的数据

时间:2017-10-17 09:41:25

标签: php mysql codeigniter csv

我创建了一个php函数,通过CodeIgniter将CSV / Excel文件导入MYSQL数据库。

  1. 它会检查数据库中是否有重复的数据行,如果还没有,它会更新数据,否则会插入数据 我的问题是当我更新CSV文件并再次上传时,我收到错误
  2.   

    严重性:警告

         

    消息:pathinfo()期望参数1为字符串,给定数组为

         

    文件名:core / Loader.php

         

    行号:759

    这是我导入CSV的编码

    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, 10000, ",")) !== FALSE)
                {
                    //check whether there are duplicate rows of data in database
                    $prevQuery = array(
                        'articleno'           => $line[0],
                        'oldarticle'          => $line[1],
                        'product_description' => $line[2],
                        'pro_type'            => $line[3],   
                        'cust_group'          => $line[4] ,
                        'size'                => $line[5] ,
                        'colour'              => $line[6],
                        'output'              => $line[7],
                        'process_description' => $line[8],
                        'material_part'       => $line[9]
    
                    );
                    $where = array(
                        'articleno'           => $line[0],
                        'process_description' => $line[8],
                        'material_part'       => $line[9]
                        );
                    $q = $this->db->select("*")
                        ->where($where)
                        ->from('sindi_productprocess_temp');
                    $prevResult = $this->db->get();
    
                    if($prevResult->num_rows > 0){
                        //update process data
                        $set =array('output'=>$line[7]);
    
                        $where = array(
                        'articleno'           => $line[0],                                  
                        'process_description' => $line[8],
                        'material_part'       => $line[9]
                        );
    
                    $query = $this->db->get_where('sindi_productprocess_temp', array('output ='=>$line[7]))->result();
    
                    }else{                
                        $data = array(
                            'articleno'           => $line[0],
                            'oldarticle'          => $line[1],
                            'product_description' => $line[2],
                            'pro_type'            => $line[3],   
                            'cust_group'          => $line[4] ,
                            'size'                => $line[5] ,
                            'colour'              => $line[6],
                            'output'              => $line[7],
                            'process_description' => $line[8],
                            'material_part'       => $line[9]
                        );
    
                        $data['crane_features']=$this->db->insert('sindi_productprocess_temp', $data);
                    }
                }
                fclose($fp) or die("can't close file");
            }
        }
    }
    

    这是我尚未更新的CSV文件 CSV file

    我想将AB02-OKE-01-09-Y0-001的输出从500更改为200,对于material_part = Top PVC Sheet-Left,以及300为Top PVC Sheet-Right

    当我更改下面的代码时,它工作但不是更新现有数据,而是插入重复数据

                if($prevResult->num_rows > 0){
                    //update process data
    
                    $data = array(
                                    'articleno'           => $line[0],
                                    'oldarticle'          => $line[1],
                                    'product_description' => $line[2],
                                    'pro_type'            => $line[3],   
                                    'cust_group'          => $line[4] ,
                                    'size'                => $line[5] ,
                                    'colour'              => $line[6],
                                    'output'              => $line[7],
                                    'process_description' => $line[8],
                                    'material_part'       => $line[9]
    
                    );
    
    $set =array('output'=>$line[7]);
    
                   $this->db->where = array(
    
                                    'articleno'           => $line[0],
                                    'oldarticle'          => $line[1],
                                    'product_description' => $line[2],
                                    'pro_type'            => $line[3],   
                                    'cust_group'          => $line[4] ,
                                    'size'                => $line[5] ,
                                    'colour'              => $line[6],
                                    'process_description' => $line[8],
                                    'material_part'       => $line[9]
                    );
    
                $this->db->update('sindi_productprocess_temp',$set);
    

0 个答案:

没有答案