CodeIgniter Forums
Unable to Import CSV - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Unable to Import CSV (/showthread.php?tid=86811)



Unable to Import CSV - Moorcam - 02-18-2023

Hi folks,
I am trying to import a CSV file and insert its contents into the database. Nothing is happening on submit. File is not uploaded, nothing in the database and no errors etc.
Can you please have a look and see if there is something I am missing?
Here is the Form (View):
Code:
<!-- Modal Start -->
<div class="modal fade" id="myModalDetail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="width:900px;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">
Import Fleet
</h4>
</div>
<div class="modal-body">
<div class="form-group">
<h3>Upload Fleet</h3>
<h4>From the options below, you can simply import your fleet quickly by uploading a spreadsheet/</h4>
<label for="exampleInputFile">Upload Excel or CSV</label>
                <?php echo form_open_multipart(base_url().'admin/fleet',array('class' => 'form-horizontal')); ?>
                    <input type="file" name="file" ><br><br>
                    <input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
                <?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>

<!-- Modal End -->

Here is the Model:
Code:
    function insert_csv($data) {
        $this->db->insert('tbl_fleet', $data);
    }

And now the Controller:
Code:
  function importcsv() {
        $data['fleet'] = $this->Model_fleet->show();
        $data['error'] = '';    //initialize image upload error array to empty

        $config['upload_path'] = './public/uploads/csv/';
        $config['allowed_types'] = 'csv';
        $config['max_size'] = '1000';

        $this->load->library('upload', $config);


        // If upload failed, display error
        if(!isset($_POST['submit'])) {
            $data['error'] = $this->upload->display_errors();

            $this->load->view('admin/fleet', $data);
        } else {
            $file_data = $this->upload->data();
            $file_path =  $file_data['./public/uploads/csv/'];
           
            if ($this->Csvimport->get_array($file_path)) {
                $csv_array = $this->Csvimport->get_array($file_path);
                foreach ($csv_array as $row) {
                    $insert_data = array(
                        'v_number'=>$row['v_number'],
                        'v_eng_number'=>$row['v_eng_number'],
'v_vin'=>$row['v_vin'],
'v_chassis'=>$row['v_chassis'],
'v_make'=>$row['v_make'],
'v_model'=>$row['v_model'],
'v_height'=>$row['v_height'],
'v_length'=>$row['v_length'],
'v_width'=>$row['v_width'],
                        'v_reg'=>$row['v_reg'],
'v_pax'=>$row['v_pax'],
'tacho_number'=>$row['tacho_number'],
'service_date'=>$row['service_date'],
'v_notes'=>$row['v_notes'],
'status'=>$row['status'],
                    );
                    $this->Model_fleet->insert_csv($insert_data);
                }
                $this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
                redirect(base_url().'csv');
                //echo "<pre>"; print_r($insert_data);
            } else
                $data['error'] = "Error occured";
                $this->load->view('admin/fleet', $data);
            }
           
        }

I am using the csvimport.php Library from Github.
Other files are uploading fine in other forms, such as Images but this one just isn't happening.
Any help you can provide would be appreciated.


RE: Unable to Import CSV - kenjis - 02-19-2023

Try this sample: https://github.com/kenjis/ci4-qb-batch-sample


RE: Unable to Import CSV - demyr - 02-20-2023

in your database table, place your id as the last column, not the first.


RE: Unable to Import CSV - Moorcam - 02-24-2023

Thanks guys.
Tried both suggestions but nothing. Thanks anyway.


RE: Unable to Import CSV - InsiteFX - 02-25-2023

Maybe this Tutorial will help.

Codeigniter 4 Import CSV Data to MySQL Database Tutorial