Recommand · October 14, 2021 0

How to prevent duplicate data when importing excel using PHPexcel in Codeigniter

public function import_excel(){
        if (!$_FILES["file"]["name"]) {
            echo "Please upload excel file !";
        } else {
            $path = $_FILES["file"]["tmp_name"];
            $object = PHPExcel_IOFactory::load($path);
            foreach ($object->getWorksheetIterator() as $worksheet) {
                $highestRow = $worksheet->getHighestRow();
                $highestColumn = $worksheet->getHighestColumn();
                for ($row = 2; $row <= $highestRow; $row++) {
                    $group = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
                    $merchant_id = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                    $login_id = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
                    $play_id = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                    $mem_name = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
                    $data[] = array(
                        'group' => $group,
                        'merchant_id' => $merchant_id,
                        'login_id' => $login_id,
                        'play_id' => $play_id,
                        'mem_name' => $mem_name,
                    );
                }
            }
            $this->db->insert_batch('excel_files', $data);
        }
    }

This code is working when upload excel, but I wonder when user upload excel 50 rows, the day after upload again 65 rows, the 65 rows no need to duplicate.