1

i am trying to insert data from the csv excel file to the database but the code seem not to work, i don't know where am doing wrong.

here is my code:

    public function postUploadS(){

    $file = array('file' => Input::file('file'));

    $rule = array('file' => 'required');
    $mime = array(  'text/csv',
                    'text/plain',
                    'application/csv',
                    'text/comma-separated-values',
                    'application/excel',
                    'application/vnd.ms-excel',
                    'application/vnd.msexcel'
                );
    $uploaded_mime = Input::file('file')->getMimeType();



    $staff_list=$_FILES['file']['tmp_name'];

    $linecount = "";
    $cols = 5;
    $numx ="";
    $mimes = array(
                    'text/csv',
                    'application/csv',
                    'text/comma-separated-values',
                    'application/excel',
                    'application/vnd.ms-excel',
                    'application/vnd.msexcel',);

        if(empty($staff_list)){
                $_SESSION['error']="<font color='#FF0000'>Please choose the csv file to upload</font>";
                }

        elseif(!in_array($_FILES['file']['type'], $mimes)) {
                $_SESSION['error']="<font color='#FF0000'>Invalid file format, Please choose only csv exel format</font>";

                }

        else{
            $staff_list=$_FILES['file']['tmp_name'];

            $handle=fopen($staff_list,"r");

            // read the first line and ignore it
            fgets($handle); 

            //count number of lines of uploaded csv file

             $fh = fopen($staff_list,'rb') or die("ERROR OPENING DATA");
                    while (fgets($fh) !== false) $linecount++;
                         fclose($fh);


            var_dump($fh); exit();

            while(($fileop=fgetcsv($handle,1000,",")) !==false){


                $fullname = $fileop[1];
                $staff_id = $fileop[0];
                $gender = $fileop[2];
                $position= $fileop[3];
                $department= $fileop[4];
                $numx = count($fileop);

                $tfulname = trim($fullname);
                $lfulname = strtolower($tfulname);

                $name_full = explode(' ', $lfulname);

                $firstname  = $name_full[0];
                $middlename = implode(array_slice($name_full, 1, -1));
                $lastname   = end($name_full);
                $phone = '';
                $email = '';
                $college = '';

                if($gender == 'M'){
                    $gender == 'Male';

                }
                elseif ($gender =='F') {
                    $gender == 'Female';
                }


        DB::beginTransaction();

    try{
        $staff = new Staff;
        $staff->staff_id = $staff_id;
        $staff->firstname = $firstname;
        $staff->middlename = $middlename;
        $staff->lastname = $lastname;
        $staff->gender = $gender;
        $staff->position = $position;
        $staff->phone = $phone;
        $staff->email = $email;
        $staff->college = $college;
        $staff->department = $department;
        $staff->save();

    }
    catch(Exception $e){

        Session::put('key','There is a duplicate row in your data,check the file and try again');
    }

        $cPass = ucfirst($lastname);    
        $hashed_pass = Hash::make($cPass);

        $user = new User;
        $user->username = $staff_id;
        $user->password = $hashed_pass;
        $user->save();


        }


        if($numx!=$cols){
                DB::rollback();
                    Session::put("error","<font color='#FF0000'>Error,number of columns does not match the defined value,please check your file and try again</font>");
                        }

        elseif(Session::has('key')){
                    DB::rollback();
                    Session::put("error","<font color='#FF0000'>Error,duplicate entry detected in your file,please check your file and try again</font>");
                    }

        else{

                DB::commit();
                Session::put("success","<font color='#0099FF'>Staff list has been uploaded successfully</font>");

                            }

} }

when i run above code no data is inserted and i don't get any error. help please

1
  • if you are using Laravel you can use "$csv->fetchAll()" like that.why didn't you try it? Commented Apr 7, 2015 at 11:04

1 Answer 1

2
$file = Reader::createFromPath('path-to-your-file');

$result = $file->fetchAll();

foreach($result as $data){
// do database add here
}
Sign up to request clarification or add additional context in comments.

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.