CodeIgniter:从excel intp现有的MySQL数据库上传数据

时间:2017-11-01 02:53:31

标签: php mysql excel codeigniter

我想从excel上传数据到我的数据库两次(来自单独的excel文件)

像这样,我的表:

    0           1            2           3      
0   44.000000   42.200001    44.799999   47.520000      
1   42.200001   44.799999    47.520000   49.760000          
2   44.799999   47.520000    49.760000   53.420000  
3   47.520000   etc.
4   49.760000   etc. 
5   53.420000

这是第一个excel数据:

id | name | address | account | note

第二个Excel

id | name | address  

这是我的控制者:

account | note

我尝试为第二个代码执行相同的代码,但是我无法将数据插入到现有行中,它总是插入到新行中。 像这样:

public function upload(){
    $this->load->helper('file');
    $fileName = time().$_FILES['file']['name'];

    $config['upload_path'] = FCPATH.'assets/'; //buat folder dengan nama assets di root folder
    $config['file_name'] = $fileName;
    $config['allowed_types'] = 'xls|xlsx|csv';
    $config['max_size'] = 10000;

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

    if(! $this->upload->do_upload('file') )
    $this->upload->display_errors();

    $media = $this->upload->data('file');

    $inputFileName = $this->upload->data('full_path');
    //'.assets/'.$media['file_name'];   

    try {
            $inputFileType = IOFactory::identify($inputFileName);
            $objReader = IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();

        for ($row = 2; $row <= $highestRow; $row++){                  //  Read a row of data into an array                 
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                            NULL,
                                            TRUE,
                                            FALSE);


            $data= array(

                "name"=> $rowData[0][1],
                "address"=> $rowData[0][2]

            );                

            $insert = $this->db->insert("info",$data);
           delete_files($config['file_path'],TRUE);                  
        }

}

任何人都可以帮助/让我知道如何加入他们吗? 任何帮助,将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:0)

制作第二个这样的excel数据

id | account | note

并更新ID为

account | note

答案 1 :(得分:0)

<?php

如果(isset($ _ POST [&#34;导入&#34;])){

    $filename=$_FILES["file"]["tmp_name"];      


     if($_FILES["file"]["size"] > 0)
     {
        $file = fopen($filename, "r");
        while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
         {


           $sql = "INSERT into table (name,address) 
               values ('".$getData[0]."','".$getData[1]."')";
               $result = mysqli_query($con, $sql);
            if(!isset($result))
            {
                echo "<script type=\"text/javascript\">
                        alert(\"Invalid File:Please Upload CSV File.\");
                        window.location = \"index.php\"
                      </script>";       
            }
            else {
                  echo "<script type=\"text/javascript\">
                    alert(\"CSV File has been successfully Imported.\");
                    window.location = \"index.php\"
                </script>";
            }
         }

         fclose($file); 
     }
}    

&GT;