0

I am using codeigniter 3 and when I uploaded data to my database, it didn't skip the duplicates.

May someone please help me to address this problem?

The code below is used to upload excel files. I'd like to skip duplicate records when a user uploads a previously uploaded excel again.

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'];
            }
        }
    }

1
  • "I'd like to skip duplicate records when a user uploads a previously uploaded excel again" - where's the code to handle that? Commented Nov 24, 2022 at 9:29

2 Answers 2

1

I think you can use array_unique() function to the variable which is storing all the data records in array format. This function returns all the unique elements and this way your duplicate data records can be removed..!

Sign up to request clarification or add additional context in comments.

Comments

0

You need to check every record form database first on the basis on primary or any combination of unique record, if it exist in db then update or skip that record else insert a new record.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.