dpmir1988 2018-03-24 14:41
浏览 69
已采纳

CodeIgniter - 将CSV上传到数据库问题

I have an application where I want a user to be able to upload a list of contacts via CSV and save them to the database. I've tried to setup brad stinsons CSV codeigniter library but been having trouble so far... When I click on upload, the CSV just disappears and goes nowhere, without any errors..

If anyone could inform me on whether I'm on the right track or just completely off I'd really appreciate it.

Here is my Csv_import.php Controller

public function load_data() {

    $result = $this->csv_import_model->select();
    $output = 'contacttable';
    $count = 0;
    if($result->num_rows() > 0)

    {
        foreach ($result->result() as $row)
        {
            $count = $count + 1;
            $output .= '
            <tr>
            <td>'.$count.'</td>
            <td>'.$row->fullname.'</td>
            <td>'.$row->email'.</td>
            <td>'.$row->country'.</td>
            <td>'.$row->gender'.</td>
            </tr>';

        }

    }
    else {
        $output .='<p>Data not available</p>';

    }
    $output .= '</table></div>';
    echo $output;


}

public function import()
{
    $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
    ["tmp_name"]);

    foreach($file_data as $row)
    {
        $data[] = array (

        'fullname' => $row["fullname"],
        'email' => $row["email"],
        'country' =>$row["country"],
        'gender' =>$row["gender"]
        );

    }
    $this->csv_import_model->insert($data);
}

my Csv_Import_model.php model

class Csv_import_model extends CI_model
{
    function select()
    {
        $this->db->order_by('idcontacts', 'DESC');
        $query = $this->db->get('contacts');
        return $query; 
    }


    function insert($data)
    {
        $this->db->insert_batch('contacts', $data);
    }
}

My View on dashboard

<form method="post" id="import_csv" enctype="multipart/form-data">
    <div class="form-group">
        <label>Select CSV File</label>
        <input type="file" name="csv_file" id="csv_file" required accept=".csv" />
    </div>

    <button type="submit" name="import_csv" class="btn btn-info" 
    id="import_csv_btn">Import CSV</button>
</form>
<br>
<div id="imported_csv_data"></div>
  • 写回答

2条回答 默认 最新

  • douzhi3105 2018-03-26 20:28
    关注

    I was able to get this working by following the git repository in this link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 springboot和springcloud版本问题
  • ¥15 ps2手柄控制树莓派小车
  • ¥30 C#:vsto powerpoint的外接程序
  • ¥30 stata将do文件代码转化为ado文件
  • ¥15 两个同维数组相比,不同位置、出现重复比无意义,而不同位置、不出现重复比,则有意义。把有意义的两个数组放入新的集合MK中。
  • ¥15 可以远程电脑安装nvm
  • ¥15 写一个可直接调用的函数,将32位有符号数转成另一个无符号的数
  • ¥15 CMAKE+VS2019+QT5.15.2组合进行二次编译
  • ¥15 nginx 配置静态html访问 ,后台登录时页面始终被重定向到登录页,无法访问到后台的静态html页
  • ¥20 自动登录的j2ee程序编译