【发布时间】:2018-04-13 08:06:20
【问题描述】:
我想将数据从 excel 上传到我的数据库两次(从单独的 excel 文件)
这样,我的桌子:
id | name | address | account | note
这是第一个excel数据:
id | name | address
第二个Excel
account | note
这是我的控制器:
public function upload(){
$this->load->helper('file');
$fileName = time().$_FILES['file']['name'];
$config['upload_path'] = FCPATH.'assets/'; //buat folder dengan nama assets di root folder
$config['file_name'] = $fileName;
$config['allowed_types'] = 'xls|xlsx|csv';
$config['max_size'] = 10000;
$this->load->library('upload');
$this->upload->initialize($config);
if(! $this->upload->do_upload('file') )
$this->upload->display_errors();
$media = $this->upload->data('file');
$inputFileName = $this->upload->data('full_path');
//'.assets/'.$media['file_name'];
try {
$inputFileType = IOFactory::identify($inputFileName);
$objReader = IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($row = 2; $row <= $highestRow; $row++){ // Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$data= array(
"name"=> $rowData[0][1],
"address"=> $rowData[0][2]
);
$insert = $this->db->insert("info",$data);
delete_files($config['file_path'],TRUE);
}
}
我尝试为第二个代码执行相同的代码,但我无法将数据插入现有行,它总是插入新行。 像这样:
id | name | address | account | note
1 a aa
2 b bb
3 1 a
4 2 bb
任何人都可以帮助/告诉我如何加入他们吗? 任何帮助,将不胜感激!谢谢!
【问题讨论】:
标签: php mysql excel codeigniter