Search code examples
phpcodeigniterphpexcel

Excel File Import using PHPExcel in codeigniter


Okay so I am trying to insert my data via using this code and as you can see here everything looks fine but still I'm getting issue like whenever I just insert one row it redirects me back to access denied and data don't get insert while when I make these rows two in excel file it inserts data immediately
if (isset($_FILES['employees_file'])) { $file = $_FILES['employees_file']['tmp_name'];

 $this->load->library('PHPExcel');
  
 $excel = PHPExcel_IOFactory::load($file);
 // $sheet = $excel->getSheet(0);
 foreach($excel->getWorksheetIterator() as $worksheet)
 {
 $highestRow = $worksheet->getHighestRow();
 $highestColumn = $worksheet->getHighestColumn();
 for($row=2; $row<=$highestRow; $row++)
 {
 $data[] = array(
 'first_name'  => ($cellValue = $worksheet->getCellByColumnAndRow(0, $row)->getValue()) == "" ? null : $cellValue,
 'last_name'  =>$worksheet->getCellByColumnAndRow(1, $row)->getValue(),
 'enroll_id'  =>$worksheet->getCellByColumnAndRow(2, $row)->getValue(),
 'nic'  =>$worksheet->getCellByColumnAndRow(3, $row)->getValue(),
 'job_title_id'  =>$worksheet->getCellByColumnAndRow(4, $row)->getValue(),
 'join_date'  =>$worksheet->getCellByColumnAndRow(5, $row)->getValue(),
 'loc_id'  =>$worksheet->getCellByColumnAndRow(6, $row)->getValue(),
 'supervisor_id'  =>$worksheet->getCellByColumnAndRow(7, $row)->getValue(),
 'dob'  =>$worksheet->getCellByColumnAndRow(8, $row)->getValue(),
 'dept_id'  =>$worksheet->getCellByColumnAndRow(9, $row)->getValue(),
 'email'  =>$worksheet->getCellByColumnAndRow(10, $row)->getValue(),
 'official_email'  =>$worksheet->getCellByColumnAndRow(11, $row)->getValue(),
 'phone'  =>$worksheet->getCellByColumnAndRow(12, $row)->getValue(),
 'address'  =>$worksheet->getCellByColumnAndRow(13, $row)->getValue(),
 'marital_status'  =>$worksheet->getCellByColumnAndRow(14, $row)->getValue(),
 'gender'  =>$worksheet->getCellByColumnAndRow(15, $row)->getValue(),
 'rel_name'  =>$worksheet->getCellByColumnAndRow(16, $row)->getValue(),
 'rel_relation'  =>$worksheet->getCellByColumnAndRow(17, $row)->getValue(),
 'rel_contact'  =>$worksheet->getCellByColumnAndRow(18, $row)->getValue(),
 'employment_status'  =>$worksheet->getCellByColumnAndRow(19, $row)->getValue(),
 'date_of_confirmation'  =>$worksheet->getCellByColumnAndRow(20, $row)->getValue(),
 'gross_salary'  =>$worksheet->getCellByColumnAndRow(21, $row)->getValue(),
 'dept_role'  =>$worksheet->getCellByColumnAndRow(22, $row)->getValue(),
 );
 }
 }
        
// array_shift($data);
$this->SqlModel->insert($data); 
}

 if ($this->db->affected_rows() > 0) {
 redirect('employees/index/success');
 } else {
 redirect(base_url().'accessdenied'); 
 }

Solution

  • I solved this issue. Actually the problem was I didn't make my columns nullable in database and that's it wasn't inserting single row of excel file but when I made all the columns nullable it started working perfectly.