Codeigniter使用excel文件导入数据

时间:2016-09-16 07:02:35

标签: php excel codeigniter codeigniter-3

我正在尝试通过exign表将数据导入到具有codeigniter应用程序的数据库中。我使用的是phpexcel。但是代码是正确的,但我收到的错误表明:

  

错误号码:1054   未知专栏' joker'在'字段列表'   INSERT INTO studentsaccount(joker)VALUES('')   文件名:C:/xampp/htdocs/Nalanda_Library/system/database/DB_driver.php   行号:691

但是我的代码如下:for controller

public function studentaccountimport(){
        $this->load->model('Department');

        $file = $_FILES['upload']['tmp_name'];
        //load the excel library
        $this->load->library('excel');
        //read file from path
        $objPHPExcel = PHPExcel_IOFactory::load($file);
        //get only the Cell Collection
        $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
        //extract to a PHP readable array format
        foreach ($cell_collection as $cell) {
            $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
            $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
            $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
            //header will/should be in row 1 only.
            if ($row == 1) {
                $header[$row][$column] = $data_value;
            } else {
                $arr_data[$row][$column] = $data_value;
                $this->Department->modeluploadation($data_value);

            }
        }
    }

代表型号:

public function modeluploadation($data){
    $this->db->insert('studentsaccount',$data);
    }

我是codeigniter的新手,所以请帮助

2 个答案:

答案 0 :(得分:0)

您需要在插入查询中指定列名称。

if ($row == 1) {
                $header[$row][$column] = $data_value;
            } else {
                $arr_data[$row][$column] = $data_value;


            }

$data['header'] = $header;
$data['values'] = $arr_data;

 $this->Department->modeluploadation($data);

答案 1 :(得分:0)

好的问题是你需要传递一组key =>值对作为insert()的参数。

我不确定为什么萨金斯的答案会因为他是对的而被标记下来。所以当你应该将modeluploadation设置为:

public function modeluploadation($data){
        $this->db->insert('studentsaccount',array('field_name'=>$data_value));
}