【问题标题】:how to skip excel sheet duplicate records when importing in database in codeigniter在 codeigniter 中导入数据库时​​如何跳过 excel 工作表重复记录
【发布时间】:2022-11-18 12:53:02
【问题描述】:

大家好,我正在使用 codeigniter 3,当我在我的数据库中上传数据时,它没有跳过重复项
你能帮我跳过重复数据吗

此代码在上传 excel 时有效,但我想知道当用户上传带有重复数据的 excel 同一张表时,再次上传后希望它跳过重复数据

下面是我的控制器

public function uploadData()
    {
        if ($this->input->post('submit')) {
            $path = 'uploads/';
            require_once APPPATH . "/third_party/PHPExcel.php";
            $config['upload_path'] = $path;
            $config['allowed_types'] = 'xlsx|xls';
            $config['remove_spaces'] = TRUE;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('uploadFile')) {
                $error = array('error' => $this->upload->display_errors());
            } else {
                $data = array('upload_data' => $this->upload->data());
            }
            if (empty($error)) {
                if (!empty($data['upload_data']['file_name'])) {
                    $import_xls_file = $data['upload_data']['file_name'];
                } else {
                    $import_xls_file = 0;
                }
                $inputFileName = $path . $import_xls_file;

                try {
                    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
                    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                    $objPHPExcel = $objReader->load($inputFileName);
                    $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
                    $flag = true;
                    $i = 0;
                    foreach ($allDataInSheet as $value) {
                        if ($flag) {
                            $flag = false;
                            continue;
                        }
                        $inserdata[$i]['SR_NO'] = $value['A'];
                        $inserdata[$i]['NTN'] = $value['B'];
                        $inserdata[$i]['NAME'] = $value['C'];
                        $inserdata[$i]['BUSINESS_NAME'] = $value['D'];

                        $i++;
                    }
                    $result = $this->import_model->importdata($inserdata);
                    if ($result) {
                        echo "Imported successfully";
                    } else {
                        echo "ERROR !";
                    }
                } catch (Exception $e) {
                    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                        . '": ' . $e->getMessage());
                }
            } else {
                echo $error['error'];
            }
        }
    }

【问题讨论】:

    标签: php mysql codeigniter-3


    【解决方案1】:

    我认为您可以对以数组格式存储所有数据记录的变量使用 array_unique() 函数。此函数返回所有唯一元素,这样可以删除重复的数据记录..!

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 2019-08-05
      • 1970-01-01
      相关资源
      最近更新 更多