Php 将csv导入两个表MYSQL-Codeigniter
通过下面的简单函数,我可以将数据导入mysql表 问题是phone列将保留在另一个表中,而今天的方式是,我只能导入到同一个表中 如何将csv文件的列导入到一个表中,将列phone导入到另一个表中Php 将csv导入两个表MYSQL-Codeigniter,php,mysql,codeigniter,Php,Mysql,Codeigniter,通过下面的简单函数,我可以将数据导入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
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
请试试这个--
使用我自己的多个插入/更新脚本进行实际的DB调用(codeigniter为您所做的),我在中读取csv文件并构建2组参数,然后进行2次mysql调用。我的一个ETL操作接收一个CSV和一个用户输入字段,并用不同的数据列组合输出4个不同的文件。我不太明白您是如何做到这一点的,我知道我可以用一种非常简单的方法来做到。但我还是不知道该怎么做。
<?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);
}