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