I want to import data from a csv file to my students table in sql.
my csv file format:
28; 2; umair; 11/4/2019; 2; islam; muslim; pk; rent; 1234567891; 11/21/2119; 111111111111; 11/20/2019; 1222; 11\11\2019; 11/28/2019; 11/20/2019; ; ; ; active; yes; yes; 11/11/1100; 11/11/0000;
every word is in a cell in excel file.
my code in StudentController.php
public function importCsv()
{
if (($handle = fopen ( public_path () . '\students.csv', 'r' )) !== FALSE) {
while ( ($data = fgetcsv ( $handle, 1000, ',' )) !== FALSE ) {
$csv_data = new Student();
$csv_data->id = $data [0];
$csv_data->bus_id = $data [1];
$csv_data->grade_id = $data [2];
$csv_data->teacher_id = $data [3];
$csv_data->room_id = $data [4];
$csv_data->name = $data [5];
$csv_data->dob = $data [6];
$csv_data->gender = $data [7];
$csv_data->religion = $data [8];
$csv_data->ethnicity = $data [9];
$csv_data->nationality = $data [10];
$csv_data->guardian_residency_type = $data [11];
$csv_data->applicant_qid_no = $data [12];
$csv_data->qid_validity = $data [13];
$csv_data->applicant_visa_no = $data [14];
$csv_data->visa_validity = $data [15];
$csv_data->passport_no = $data [16];
$csv_data->passport_expiry = $data [17];
$csv_data->session_start_date = $data [18];
$csv_data->estimated_graduation_date = $data [19];
$csv_data->bus_service = $data [20];
$csv_data->bus_pickup = $data [21];
$csv_data->bus_dropoff = $data [22];
$csv_data->created_at = $data [23];
$csv_data->updated_at = $data [24];
$csv_data->deleted_at = $data [25];
$csv_data->save ();
}
fclose ( $handle );
}
}
I am calling importCsv function through a button in my view file:
<a href="{{ url('admin/auth/student/importCsv') }}" class="btn btn-xs btn-info pull-right">Import as CSV</a>
The students.csv is already stored in the public directory of the project with the data in it.
I keep on getting different errors every time I refresh with whatever possible change I think is necessary but now I'm stuck. The latest error is
Unexpected data found. Unexpected data found. A two digit minute could not be found
I think it's all due to some sort of format error but I have taken the data from the database as a sample which is working totally fine when added through the backend by user.