在数据库中导入CSV文件

时间:2014-05-13 03:28:51

标签: php codeigniter csv

您好我如何将csv文件插入数据库。该表格包含以下列。

database: appwarehouse
table:application table
column: app_id, app name

这是我的视图代码

<form action="<?php echo base_url(); ?>some_controller/uploadBulk" method="post" enctype="multipart/form-data">     
    <label for="exampleInputFile">File Upload</label>
    <input type="file" name="file" id="file"><br><br>
    <input type="submit" name="submit" value="Upload" class="classname"><br>            
</form>

这是我的控制器代码

public function uploadBulk(){
    $this->load->model('some_model');
    /*
        * similar to $name = $_POST['name'];
    */
    $file = $this->input->post('file');

    $success = $this->some_model->insertBulkApp($file);
    /* i DONT REALLY KNOW THE CODE JUST INSERT SOME CODE IN HERE */

    if($success == TRUE)
        $this->insert_page(TRUE);
    else 
        $this->insert_page(FALSE);
}

我还需要我的模型的代码。

1 个答案:

答案 0 :(得分:0)

模型中的代码可以非常简单

public function insertBulkApp($file) {
    // Name of the table you want to insert to
    $this->db->insert('application table', $file);
}

在您的控制器中,您可以设置一个数组,其中键将等于要插入的表的列名称

 // Column name of the table you want to insert to
 $file["app name"] = $this->input->post('file');
 $this->some_model->insertBulkApp($file);

修改

您可以在线查看文档,了解有关codeigniter的更多信息。 Codeigniter documentation