簡體   English   中英

如何通過在 PHPExcel Codeigniter3 中上傳 excel 文件來更新數據 mysql 數據庫

[英]How to Update Data mysql database by uploading excel file in PHPExcel Codeigniter3

我閱讀了許多導入 excel 以在 Mysql 數據庫中插入數據,但我沒有找到如何通過上傳 excel 文件更新數據的參考資料。 那么,當我導入數據 excel 文件時,如何更新 mysql 數據庫中的數據?

示例:在數據庫表 A 中:

Id_Follow_Up Tgl_Fol_Up
1個 2022-10-30
2個 2022-10-29

Excel

Id_Follow_Up Tgl_Fol_Up
1個 2022-11-30
2個 2022-11-30

所以,我想用 Excel 中的新數據更新我的表。基於DB ID 1 的示例是 2022-10-30,但我想更新基於 excel 的數據,所以,我想將 ID 1 從 2022-10-30 更改為 2022- 11-30。 同樣,ID 2 從 2022-10-29 到 2022-11-30。

注意:我使用 PHPExcel

這是我的 Controller 代碼:

public function import_excel(){
    if(isset($_FILES["fileExcel"]["name"])){
      $path = $_FILES["fileExcel"]["tmp_name"];
      $object = PHPExcel_IOFactory::load($path);
      foreach($object->getWorksheetIterator() as $worksheet){
        $highestRow = $worksheet->getHighestRow();
                $highestColumn = $worksheet->getHighestColumn();
        for($row=2; $row<=$highestRow; $row++)
        {
          $tgl_fol_up = $worksheet->getCellByColumnAndRow(1,$row)->getValue();
          $tgl_fol_up = \PHPExcel_Style_NumberFormat::toFormattedString($tgl_fol_up, 'YYYY-MM-DD');
          $id_follow_up = $worksheet->getCellByColumnAndRow(0,$row)->getValue();
          $temp_data[]=array(
            'tgl_fol_up' => $tgl_fol_up
          );
           $where[]=array(
            'id_follow_up' => $id_follow_up
          );
        }
      }
    
      $update = $this->Customer_list_model->update_import($where,$temp_data);
      if($update)
      {
        $_SESSION['pesan']  = "Data Berhasil di Import ke Database";
        $_SESSION['tipe']   = "success";
                redirect($_SERVER['HTTP_REFERER']);
      }else{
        $_SESSION['pesan']  = "Terjadi Kesalahan";
        $_SESSION['tipe']   = "danger";
        redirect($_SERVER['HTTP_REFERER']);
      }
    }else{
      echo "Tidak Ada File yang masuk";
    }
  }

這是我的 Model 代碼:

public function update_import($where,$temp_data)
{
        $this->db->where($where);
         $insert = $this->db->insert_batch('data_detail', $temp_data);
        if($insert){
            return true;
        }
}

請幫我。

我做到了。 我解決了 Model 只是刪除和 controller 我添加和更改代碼如下:

public function import_excel(){
    if(isset($_FILES["fileExcel"]["name"])){
      $path = $_FILES["fileExcel"]["tmp_name"];
      $object = PHPExcel_IOFactory::load($path);
      foreach($object->getWorksheetIterator() as $worksheet){
        $highestRow = $worksheet->getHighestRow();
                $highestColumn = $worksheet->getHighestColumn();
        for($row=2; $row<=$highestRow; $row++)
        {
          $tgl_fol_up = $worksheet->getCellByColumnAndRow(1,$row)->getValue();
          $tgl_fol_up = \PHPExcel_Style_NumberFormat::toFormattedString($tgl_fol_up, 'YYYY-MM-DD');
          $id_follow_up = $worksheet->getCellByColumnAndRow(0,$row)->getValue();
          $temp_data=array(
            'tgl_fol_up' => $tgl_fol_up,
            'id_follow_up '  => $id_follow_up 
          );
          $this->db->where('id_follow_up', $id_follow_up);
          $update = $this->db->update('data_detail',$temp_data);
        }
      }
    
      $update = $this->Customer_list_model->update_import($where,$temp_data);
      if($update)
      {
        $_SESSION['pesan']  = "Data Berhasil di Import ke Database";
        $_SESSION['tipe']   = "success";
                redirect($_SERVER['HTTP_REFERER']);
      }else{
        $_SESSION['pesan']  = "Terjadi Kesalahan";
        $_SESSION['tipe']   = "danger";
        redirect($_SERVER['HTTP_REFERER']);
      }
    }else{
      echo "Tidak Ada File yang masuk";
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM