【问题标题】:CodeIgniter: Upload data from excel intp existing MySQL databaseCodeIgniter:将数据从 excel 上传到现有的 MySQL 数据库
【发布时间】:2018-04-13 08:06:20
【问题描述】:

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

这样,我的桌子:

id | name | address | account | note

这是第一个excel数据:

id | name | address  

第二个Excel

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);                  
        }

}

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

id | name | address | account | note
1     a       aa  
2     b       bb
3                       1       a
4                       2       bb

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

【问题讨论】:

    标签: php mysql excel codeigniter


    【解决方案1】:

    像这样制作第二个excel数据

    id | account | note
    

    并使用 id 更新 account | note

    【讨论】:

    • 我试过了,但它给了我一个错误:重复条目 '1' for key 'PRIMARY' UPDATE info SET id = 1, note = 1
    • 或者我应该把更新列的'id'放在哪里
    【解决方案2】:
    <?php
    

    if(isset($_POST["Import"])){

        $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); 
         }
    }    
    

    ?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多