将csv导入两个表mysql-codeigniter

fd3cxomn  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(274)

通过下面的简单函数,我可以将数据导入mysql表。
问题是phone列将保留在另一个表中,而今天的方式是,我只能导入到同一个表中。
如何将csv文件的列导入到一个表中,并将列phone导入到另一个表中?

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);
}

table

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
v2g6jxz6

v2g6jxz61#

请试试这个--

<?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);
}

相关问题