今天我遇到了一个功能,我需要通过codeigniter 将CSV / Excel文件导入MYSQL数据库。客户有一个特殊要求,他可以上传CSV HTML表格中的文件上传字段中的/ Excel文件,CSV / Excel中的所有数据必须通过PHP导入MYSQL 数据库表并删除重复的数据行但我在这里遇到了一些问题。
我的问题是:
文件停止导入数据时发生此错误
这是我的模特
<?php
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)) !== FALSE){
//check whether there are duplicate rows of data in database
$prevQuery = array(
'articleno'=> $line[0] ,
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]
);
$q=$this->db->select('sindi_productprocess_temp', $prevQuery)
->where('articleno',$line[0],
'product_description', $line[1] ,
'cust_name' , $line[2] ,
'size', $line[3] ,
'colour' , $line[4],
'process_description' , $line[5],
'output', $line[6],
'material_part', $line[7],
'printingOutput', $line[8]);
$prevResult = $this -> db->query($q);
if($prevResult->num_rows > 0){
//update process data
$data = array(
'articleno' => $line[0] ,
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]
);
$this->db->set
(
'articleno',$line[0],
'product_description', $line[1] ,
'cust_name' , $line[2] ,
'size', $line[3] ,
'colour' , $line[4],
'process_description' , $line[5],
'output', $line[6],
'material_part', $line[7],
'printingOutput', $line[8]
);
$this->db-where('articleno',$line[0],
'product_description', $line[1] ,
'cust_name' , $line[2] ,
'size', $line[3] ,
'colour' , $line[4],
'process_description' , $line[5],
'output', $line[6],
'material_part', $line[7],
'printingOutput', $line[8]
);
$this->db->update('sindi_productprocess_temp');
}else{
for($i = 0, $j = count($line); $i < $j; $i++){
$data = array('articleno' => $line[0] ,
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]
);
$data['crane_features']=$this->db->insert('sindi_productprocess_temp', $data);
}
$i++;
}
}
fclose($fp) or die("can't close file");
}
}
function get_car_features_info(){
$get_cardetails=$this->db->query("select * from sindi_productprocess_temp");
return $get_cardetails;
}
}
?>
这是我的控制器
function processupload(){
$this->load->model('upload_services');
$data['result']=$this->upload_services->upload_sampledata_csv();
$data['query']=$this-> upload_services->get_car_features_info();
$this->load->view(' Upload_csv ',$data);
}
这是我的观点
<form action="{$path}pro/processupload" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table>
<tr>
<td> Choose your file: </td>
<td><input type="file" class="form-control" name="userfile" id="userfile" align="center"/></td>
<td><div class="modal-footer">
<td colspan="2" ><input type="submit" id="submit" name="submit" value="Import"></td>
</div>
</td>
</tr>
</table>
有人可以帮我吗? 谢谢。
答案 0 :(得分:1)
有三个错误:
get()
而不是query()
,我们在编写完整的自定义查询时使用查询。for($i = 0, $j = count($line); $i < $j; $i++)
添加重复记录我还添加了两条评论:
以下是您的模型的完整代码:
<?php
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");
//fgetcsv($fp, 1000, ",");// to remove header from CSV
/*** If you want associated Array *****/
/*
$csv = array_map("str_getcsv", file($_FILES['userfile']['tmp_name'],FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
//To turn all the rows into a nice associative array you could then apply:
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
*/
while(($line = fgetcsv($fp)) !== FALSE)
{
//check whether there are duplicate rows of data in database
$prevQuery = array(
'articleno'=> $line[0] ,
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]
);
$where = array('articleno' => $line[0],
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]);
$q = $this->db->select("*")
->where($where)
->from('sindi_productprocess_temp');
$prevResult = $this->db->get();
if($prevResult->num_rows > 0){
//update process data
$data = array(
'articleno' => $line[0] ,
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]
);
$this->db->set
(
'articleno',$line[0],
'product_description', $line[1] ,
'cust_name' , $line[2] ,
'size', $line[3] ,
'colour' , $line[4],
'process_description' , $line[5],
'output', $line[6],
'material_part', $line[7],
'printingOutput', $line[8]
);
$this->db-where
(
'articleno',$line[0],
'product_description', $line[1] ,
'cust_name' , $line[2] ,
'size', $line[3] ,
'colour' , $line[4],
'process_description' , $line[5],
'output', $line[6],
'material_part', $line[7],
'printingOutput', $line[8]
);
$this->db->update('sindi_productprocess_temp');
}else{
$data = array(
'articleno' => $line[0] ,
'product_description' => $line[1] ,
'cust_name' => $line[2] ,
'size' => $line[3] ,
'colour' => $line[4],
'process_description' => $line[5],
'output' => $line[6],
'material_part' => $line[7],
'printingOutput' => $line[8]
);
$data['crane_features']=$this->db->insert('sindi_productprocess_temp', $data);
}
}
fclose($fp) or die("can't close file");
}
}
function get_car_features_info()
{
$get_cardetails=$this->db->query("select * from sindi_productprocess_temp");
return $get_cardetails;
}
}
?>
答案 1 :(得分:0)
尝试此代码:-
控制器文件功能
function import_flat(){
if(isset($_POST['submit'])){
$fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
while(($line = fgetcsv($fp)) !== FALSE){
$query = $this->db->get_where('flat', array('name' => $line[0] ,'wingid' => $line[1]));
if($query->num_rows() > 0){
//update process data
$data = array('name' => $line[0] ,'wingid' => $line[1]);
$this->db->where('wingid', $line[1]);
$this->db->where('name', $line[0]);
$this->db->update('flat', $data);
echo "Data update";
}
else
{
for($i = 1; $i < count($line); $i++)
{
$data = array('name' => $line[0] ,'wingid' => $line[1]);
print_r($data);
print_r(count($line));
$data['flats']=$this->db->insert('flat', $data);
}
$i++;
}
}
fclose($fp) or die("can't close file");
}
}
查看文件
<?php echo form_open_multipart('Flat/flats/importflat', array('method' => 'post')); ?>
<div class="form-group">
<label>Excel File For Flat</label>
<br>
<span class="btn btn-primary fileinput-button">
<i class="fa fa-plus"></i>
<span>Add file</span>
<input class="form-control" type="file" name="userfile" id="userfile" required>
</span>
</div>
<button type="submit" class="mb-sm btn btn-primary" id="submit" name="submit">Submit</button>
<?php echo form_close(); ?>