将CSV导入到两个表MYSQL-Codeigniter

时间:2018-10-21 00:22:19

标签: php mysql codeigniter

通过下面的简单功能,我可以将数据导入到mysql表中。

问题在于,“电话”列将保留在另一个表中,而今天,我只能导入到同一表中。

如何将csv文件的列导入到一个表中,将电话列导入到另一个表中?

public function upload_file(){
    $csvMimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){

            //open uploaded csv file with read only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], 'r');

            // skip first line
            // if your csv file have no heading, just comment the next line
            fgetcsv($csvFile);

            //parse data from csv file line by line
            while(($line = fgetcsv($csvFile)) !== FALSE){
                //check whether member already exists in database with same email
                $result = $this->db->get_where("tb_person", array("email"=>$line[1]))->result();
                if(count($result) > 0){
                    //update person data
                    $this->db->update("tb_person", array("name"=>$line[0], "phone"=>$line[2], "created"=>$line[3], "status"=>$line[4]), array("email"=>$line[1]));
                }else{
                    //insert person data into database
                    $this->db->insert("tb_person", array("name"=>$line[0], "email"=>$line[1], "phone"=>$line[2], "created"=>$line[3], "status"=>$line[4]));
                }
            }

            //close opened csv file
            fclose($csvFile);

            $qstring["status"] = 'Success';
        }else{
            $qstring["status"] = 'Error';
        }
    }else{
        $qstring["status"] = 'Invalid file';
    }
    $this->load->view('csvToMySQL',$qstring);
}

表格

tb_person
name varchar(100) NOT NULL
email varchar(100) NOT NULL
phone varchar(100) NOT NULL
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
status varchar(100) NOT NULL

tb_phone
phone varchar(100) NOT NULL
person_id int(11) NOT NULL

1 个答案:

答案 0 :(得分:1)

请尝试--

<?php


public function upload_file(){
    $csvMimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){

            //open uploaded csv file with read only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], 'r');

            // skip first line
            // if your csv file have no heading, just comment the next line
            fgetcsv($csvFile);

            //parse data from csv file line by line
            while(($line = fgetcsv($csvFile)) !== FALSE){
                //check whether member already exists in database with same email
                $result = $this->db->get_where("tb_person", array("email"=>$line[1]))->result();
                if(count($result) > 0){
                    $person_id = $result[0]['id'];
                    //update person data
                    $this->db->update("tb_person", array("name"=>$line[0], "phone"=>$line[2], "created"=>$line[3], "status"=>$line[4]), array("email"=>$line[1]));
                }else{
                    //insert person data into database
                    $this->db->insert("tb_person", array("name"=>$line[0], "email"=>$line[1], "phone"=>$line[2], "created"=>$line[3], "status"=>$line[4]));
                    $person_id = $this->db->insert_id();
                }
                $this->db->insert("tb_phone",array('phone' => $line[2],'person_id' => $person_id));
            }

            //close opened csv file
            fclose($csvFile);

            $qstring["status"] = 'Success';
        }else{
            $qstring["status"] = 'Error';
        }
    }else{
        $qstring["status"] = 'Invalid file';
    }
    $this->load->view('csvToMySQL',$qstring);
}