php - 如何编写条件来检查不在 codeigniter 数据库表中的 csv 文件数据?

标签 php mysql codeigniter csv

我的 Controller 看起来

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

我可以更新 menu 表内容,其中 mid 等于 csv 文件 mid ($data1['mid'] )。它工作正常。

我的需要是我必须在 csv 文件 mid 中插入记录,而不是在菜单表 中。为此,我使用了一个条件

    if($this->db->where("mid <>",$data1['mid']))
    {
        $this->db->insert('menu', $data1);
    }

但它不工作,它工作时如果csv 文件 中的mid 不在菜单表 中,然后它插入整个csv 文件内容放入表中。

我的需要是只插入不在菜单表中的记录。那个条件怎么写。我已经为此工作了很长时间,我对这种情况很陌生。提前致谢。

最佳答案

首先我想建议的是,

您在 foreach 循环中执行大量数据库操作,这对性能因素不利。您应该先准备查询,然后立即触发。

另一件事是您正在通过 PHP 代码检查数据库中的记录并触发查询。而不是你应该只检查 MySQL。

下面是一个粗略的解决方案,您可以根据自己的参数实现。

您可以编写一个查询来检查表中的现有记录(如果不存在),然后将其插入到表中。

WHERE NOT EXISTS

这可以在您的查询中使用。

还有一个,您可以附加更多查询并使用 insert_batch 立即触发它代码点火器的功能。

粗略的结构是这样的

foreach(x as y) {
     //push all data to array $queries;
}
$this->db->insert_batch('menu',$queries);

关于php - 如何编写条件来检查不在 codeigniter 数据库表中的 csv 文件数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787550/

相关文章:

PHP -\u00f8 (ø) 变为 u00f8

php - Fuel CMS如何通过Simple Modules更新数据库?

javascript - 使用 Codeigniter 在模态中使用 jquery 显示图像

MySQL - varchar 的多种情况

mysql - 限制sql中的计数

php - MySQL:设计具有主数据库和辅助数据库的应用程序

php - 虚拟主机在 ubuntu LAMP 堆栈中无法正常工作

php - 我们的脚本生成的文件夹/文件没有适当的权限

php - 如何在 Silverstripe 中对 $AbsoluteLink 使用不同的协议(protocol)

php - 使用 PHP 和 MySQL 如何使用另一个表中的某些字段值填充一个表