【发布时间】:2016-12-11 09:31:36
【问题描述】:
我的需要是每次我上传 csv 文件时我都需要更新数据库表,我的意思是如果 csv 文件中的 mid 存在于数据库 mid我必须更新该行。如果 csv 文件中的 mid 在数据库 mid 中不存在,我必须将该新行插入到数据库表中。
目前我成功上传了 csv 文件和存储在数据库表中的数据。
我的控制器看起来像:
public function importcsv()
{
$data['menu'] = $this->AdminModel->get_menu();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
}
else
{
$file_data = $this->upload->data();
$file_path = './assets/uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path))
{
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row)
{
$mid=$row['mid'];
$insert_data = array(
'mid'=>$mid,
'category'=>$row['category'],
'name'=>$row['name'],
'price'=>$row['price'],
'description'=>$row['description'],
);
$this->AdminModel->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'Admin/menu');
}
else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
我认为在将 csv 文件数据提取到数组 $insert_data 之后,我认为我需要将其与数据库中的 mid 进行比较桌子。这怎么可能?我是新来的。预先感谢。
我对控制器进行了修改,它的外观:
public function importcsv()
{
$data['menu'] = $this->AdminModel->get_menu();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
}
else
{
$file_data = $this->upload->data();
$file_path = './assets/uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path))
{
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row)
{
$data1['mid']=$row['mid'];
$data1['category']=$row['category'];
$data1['name']=$row['name'];
$data1['price']=$row['price'];
$data1['description']=$row['description'];
if($this->db->where('mid', $data1['mid']))
{
$this->db->update('menu', $data1);
}
if($this->db->where("mid !=",$data1['mid']))
{
$this->db->insert('menu', $data1);
}
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'Admin/menu');
}
else
{
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
}
现在我可以更新数据了
if($this->db->where('mid', $data1['mid']))
{
$this->db->update('menu', $data1);
}
它工作正常,但使用条件:
if($this->db->where("mid !=",$data1['mid']))
{
$this->db->insert('menu', $data1);
}
整个 csv 文件数据被重新输入到表格菜单中。我需要比较 menu 表中的 mid 和 csv 文件中的 mid,如果 mid 在 >csv 文件 不存在于 menu 表中,我需要将相应的记录插入到 menu 表中。
为此,我应该对代码进行哪些更改?
【问题讨论】:
-
那么
mid是唯一的吗? -
在插入一条记录之前,可以检查是否存在相同的mid。如果存在更新,否则插入。您可以使用单个 sql 查询来完成此操作
-
你应该让它成为数据库中的唯一键然后你可以使用
insertOnDuplicate -
@Anish 我尝试了很多,但还不行你可以添加查询来检查条件吗?
-
对于更新,您将一一比较记录。如果没有很多记录,最好的方法是删除所有记录并重新插入。 (可能主键值会改变)。
标签: php codeigniter csv