2016-08-03 158 views
0

我是codeigniter的新手,我正在做以下的事情,但我沒有得到解決方案。在mysql codeigniter中插入和更新一個查詢

以下是一件一件的事情。

  1. 首先我從數據庫中獲取數據並通過foreach循環以表格格式顯示。 $''''''''''''''''''''''''''''''''); echo form_open('emp_hodm_update/update_hodm',$ attributes); ? >

    日期 工作 合作伙伴 主任 時間 任務 狀態 行動 日期;?> 「類= 」「/> 工作;?>」 class =「」/> partner;?>「class =」「/> 主任;?> 「類= 」「/> 時間;?>」 類= 「」/> 任務;?> 狀態;?> 刪除

  2. 通過點擊添加按鈕它會創建新的行所以我想更新舊的數據以及通過添加buttond插入到表中創建的新行數據。

  3. 但我沒有得到任何想法如何做到這一點。

控制器:

public function update_hodm(){ 

/* Checking the all validation of task form*/ 
//$this->form_validation->set_rules('date', 'Date', 'required'); 
$this->form_validation->set_rules('work[]', 'Types of Work', 'required'); 
//$this->form_validation->set_rules('partner[]', 'Worked With', 'required'); 
$this->form_validation->set_rules('director[]', 'Director', 'required'); 
$this->form_validation->set_rules('time[]', 'No Of Hours', 'required'); 
$this->form_validation->set_rules('task[]', 'Task Details', 'required'); 
$this->form_validation->set_rules('status[]', 'Task Status', 'required'); 

if ($this->form_validation->run()) { 
    /* Taking the data from form*/ 
    $todayDate = date('Y-m-d'); 
    $work=$this->input->post('work'); 
    $partner=$this->input->post('partner'); 
    $director=$this->input->post('director'); 
    $time=$this->input->post('time'); 
    $task=$this->input->post('task'); 
    $status=$this->input->post('status'); 

    $count=count($this->input->post('work')); 
    $data =array(); 
    for($i=0; $i<$count; $i++) { 
    $data[$i] = array(
     'name'  =>$this->session->userdata('emp_name'), 
     'date'  =>$todayDate, 
     'work'  =>$work[$i], 
     'partner' =>$partner[$i], 
     'director' =>$director[$i], 
     'time'  =>$time[$i], 
     'task'  =>$task[$i], 
     'status'  =>$status[$i] 
     ); 
     } 
     $add=$this->update->update_hodm($data,$todayDate); 
     /* Display Success message if data updated successfully in database*/ 

      if($add){ 
       $this->session->set_flashdata('hodm_form',"All HODM Data Inserted Successfully."); 
       $this->session->set_flashdata('hodm_form_class','alert-success'); 
      }else{ 
       /* Displaying the error message*/ 
       $this->session->set_flashdata('hodm_form',"failed to add, Please Try again"); 
       $this->session->set_flashdata('hodm_form_class','alert-danger'); 
      } 
     return redirect('home'); 
} else { 
    $this->load->view('public/digital_hodm_view'); 
} 
} 

型號:

public function update_hodm($data,$todayDate){ 
     $this->db->where('date',$todaydate); 
     $this->db->update_batch('task_form', $data,'date');//date is my table column name 
     return true; 
    } 
  • 請幫我找到解決方案。
  • 預先感謝

    +0

    爲什麼單擊添加按鈕時需要更新舊數據?只需在控制器中創建一個添加功能,並在添加按鈕被點擊時調用該功能 – Sultan

    +0

    您實際上沒有提到問題所在!除了_但我沒有得知任何想法如何做到這一點。在這種情況下,解決方案是擊中書籍 – RiggsFolly

    +0

    @蘇丹你沒有得到我。當我點擊添加按鈕,然後它創建同一列的新行空白。在填充新行並在舊行更改時,當我單擊提交按鈕時,它會進入帶有舊數據和新數據的表格 – xr33dx

    回答

    0

    使用下面的代碼視圖中看到 增加一個列的updated_at

    查看:

    <?php 
         $attributes = array('class' => 'form-horizontal','id'=>'update_form'); 
         echo form_open('emp_hodm_update/update_hodm', $attributes); 
         ?> 
         <table class="table table-striped table-hover table-responsive"> 
         <thead> 
         <tr class=""> 
          <th>date</th> 
          <th>Work</th> 
          <th>Partner</th> 
          <th>Director</th> 
          <th>Time</th> 
          <th>Task</th> 
          <th>Status</th> 
          <th>Action</th> 
         </tr> 
         </thead> 
         <tbody> 
         <?php foreach($result as $row) { ?> 
          <tr> 
          <td> 
           <input class="form-control" type="text" name="date[]" value="<?php echo $row->date;?>" class="" /> 
          </td> 
          <td> 
           <input class="form-control" type="text" name="work[]" value="<?php echo $row->work;?>" class="" /> 
          </td> 
          <td> 
           <input class="form-control" type="text" name="partner[]" value="<?php echo $row->partner;?>" class="" /> 
          </td> 
          <td> 
           <input class="form-control" type="text" name="director[]" value="<?php echo $row->director;?>" class="" /> 
          </td> 
          <td> 
           <input class="form-control" type="text" name="time[]" value="<?php echo $row->time;?>" class="" /> 
          </td> 
          <td> 
           <textarea class="form-control" name="task[]" rows="2" id=""><?php echo $row->task;?></textarea> 
          </td> 
          <td> 
           <textarea class="form-control" name="status[]" rows="2" id=""><?php echo $row->status;?></textarea> 
          </td> 
          <td> 
           <a href="" class="btn btn-danger">Delete</a> 
    
        <!-- Add This --> 
           <input class="form-control" type="hidden" name="form_id_hidden[]" value="<?php echo $row->form_id;?>" 
    
          </td> 
          </tr> 
         <?php } ?> 
         </tbody> 
         </table> 
         <input class="btn btn-primary" type="submit" name="submit" value="submit"> 
         <input class="btn btn-primary" type="submit" name="submit" value="add"> 
         <?php 
         echo form_close(); 
         ?> 
    

    控制器:

    public function update_hodm(){ 
    
        /* Checking the all validation of task form*/ 
        //$this->form_validation->set_rules('date', 'Date', 'required'); 
        $this->form_validation->set_rules('work[]', 'Types of Work', 'required'); 
        //$this->form_validation->set_rules('partner[]', 'Worked With', 'required'); 
        $this->form_validation->set_rules('director[]', 'Director', 'required'); 
        $this->form_validation->set_rules('time[]', 'No Of Hours', 'required'); 
        $this->form_validation->set_rules('task[]', 'Task Details', 'required'); 
        $this->form_validation->set_rules('status[]', 'Task Status', 'required'); 
    
        if ($this->form_validation->run()) { 
         /* Taking the data from form*/ 
           $todayDate = date('Y-m-d'); 
    
           <!-- Add This --> 
           $updatedDate = date('Y-m-d H:i:s'); // if not used H:i:s and there is no chnage in data update will return false 
    
           $work=$this->input->post('work'); 
           $partner=$this->input->post('partner'); 
           $director=$this->input->post('director'); 
           $time=$this->input->post('time'); 
           $task=$this->input->post('task'); 
           $status=$this->input->post('status'); 
    
           <!-- Add This --> 
           $form_id=$this->input->post('form_id_hidden'); 
    
           $count=count($this->input->post('work')); 
    
           <!-- Add This --> 
           $data = $new_data = array(); 
           $old_ids = array(); 
    
           for($i=0; $i<$count; $i++) { 
    
            <!-- Add This --> 
    
            if(!in_array($form_id[$i],$old_ids)){ 
             $old_ids[] = $form_id[$i]; 
    
           $data[$i] = array(
            'name'  =>$this->session->userdata('emp_name'), 
            'date'  =>$todayDate, 
            'work'  =>$work[$i], 
            'partner' =>$partner[$i], 
            'director' =>$director[$i], 
            'time'  =>$time[$i], 
            'task'  =>$task[$i], 
            'status'  =>$status[$i], 
            'form_id' =>$form_id[$i], 
            'updated_at' =>$updatedDate, 
            ); 
           }else{ 
            $new_data[$i] = array(
            'name'  =>$this->session->userdata('emp_name'), 
            'date'  =>$todayDate, 
            'work'  =>$work[$i], 
            'partner' =>$partner[$i], 
            'director' =>$director[$i], 
            'time'  =>$time[$i], 
            'task'  =>$task[$i], 
            'status'  =>$status[$i], 
            ); 
           } 
          } 
    
          <!-- Add This --> 
    
          $update=$this->update->update_hodm($data,'form_id'); 
          $add = $this->update->addnew_hodm($new_data); 
          /* Display Success message if data updated successfully in database*/ 
    
            if($update && $add){ <!-- Add This --> 
              $this->session->set_flashdata('hodm_form',"All HODM Data Updated and Inserted Successfully."); 
              $this->session->set_flashdata('hodm_form_class','alert-success'); 
    
             }else if($update){ <!-- Add This --> 
              $this->session->set_flashdata('hodm_form',"All HODM Data Updated Successfully."); 
              $this->session->set_flashdata('hodm_form_class','alert-success'); 
             } 
             else if($add){ 
              $this->session->set_flashdata('hodm_form',"All HODM Data Inserted Successfully."); 
              $this->session->set_flashdata('hodm_form_class','alert-success'); 
             }else{ 
              /* Displaying the error message*/ 
              $this->session->set_flashdata('hodm_form',"failed to add, Please Try again"); 
              $this->session->set_flashdata('hodm_form_class','alert-danger'); 
             } 
          return redirect('home'); 
        } else { 
         $this->load->view('public/digital_hodm_view'); 
        } 
        } 
    
    Model : 
    
    <!-- Add This --> 
        public function addnew_hodm($data){ 
         if(empty($data)){ 
          return false; 
         } 
         $this->db->insert_batch('task_form', $data); 
         return ($this->db->affected_rows()>0?TRUE:FALSE); 
        } 
    
        <!-- Add This --> 
        public function update_hodm($data,$where){ 
         if(empty($data)){ 
          return false; 
         } 
         $this->db->update_batch('task_form', $data,$where); 
         return ($this->db->affected_rows()?TRUE:FALSE); 
        } 
    
    +0

    。謝謝你的答案。它的工作現在.. – xr33dx

    +0

    hmmmm歡迎好友 –

    0

    如果有獨特之處在於表/主鍵,添加密鑰爲每個數據。假設關鍵是task_id,更新您的控制器代碼:

    ... 
        ... 
        ... 
        'task'  =>$task[$i], 
        'status'  =>$status[$i] 
        ); 
        if(!empty($task_id[$i]))$data[$i]['task_id'] = $task_id[$i]; //Add task ids for old data 
        } 
        $add=$this->update->update_hodm($data,$todayDate); 
    

    在模型中,更新與下面的代碼功能:

    foreach($data as $array) 
    { 
        $query = 'INSERT INTO `table` SET '; 
        $sep = $setvalues = ''; 
        foreach($array as $key=>$value) { 
        $setvalues .= $sep.$key.' = "'.$value.'"'; 
        $sep = ','; 
        } 
        $query .= $setvalues.' ON DUPLICATE KEY UPDATE '.$setvalues.';';  
        $this->db->query($query); 
    } 
    
    0

    VIEW:

    <?php 
    $attributes = array(
        'class' => 'form-horizontal', 
        'id' => 'update_form' 
    ); 
    echo form_open('emp_hodm_update/update_hodm', $attributes); 
    ?> 
    <table class="table table-striped table-hover table-responsive"> 
    <thead> 
    <tr class=""> 
        <th>date</th> 
        <th>Work</th> 
        <th>Partner</th> 
        <th>Director</th> 
        <th>Time</th> 
        <th>Task</th> 
        <th>Status</th> 
        <th>Action</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php 
    
    foreach($result as $row) 
        { ?> 
        <tr> 
        <td> 
         <input class="form-control" type="hidden" name="form_id[]" value="<?php 
        echo $row->form_id; ?>" class="" /> 
         <input class="form-control" type="text" name="date[]" value="<?php 
        echo $row->date; ?>" class="" /> 
        </td> 
        <td> 
         <input class="form-control" type="text" name="work[]" value="<?php 
        echo $row->work; ?>" class="" /> 
        </td> 
        <td> 
         <input class="form-control" type="text" name="partner[]" value="<?php 
        echo $row->partner; ?>" class="" /> 
        </td> 
        <td> 
         <input class="form-control" type="text" name="director[]" value="<?php 
        echo $row->director; ?>" class="" /> 
        </td> 
        <td> 
         <input class="form-control" type="text" name="time[]" value="<?php 
        echo $row->time; ?>" class="" /> 
        </td> 
        <td> 
         <textarea class="form-control" name="task[]" rows="2" id=""><?php 
        echo $row->task; ?></textarea> 
        </td> 
        <td> 
         <textarea class="form-control" name="status[]" rows="2" id=""><?php 
        echo $row->status; ?></textarea> 
        </td> 
        <td> 
         <a href="" class="btn btn-danger">Delete</a> 
        </td> 
        </tr> 
        <!-- 
        start :if extra row is add on the button click 
        --> 
        <tr> 
         <td> 
          <input class="form-control" type="text" name="date[]" value="" class="" /> 
          <!-- 
          make sure at time of addition value of form_id[] should be empty 
          --> 
          <input class="form-control" type="hidden" name="form_id[]" value="" class="" /> 
         </td> 
         <td> 
          <input class="form-control" type="text" name="work[]" value="" class="" /> 
         </td> 
         <td> 
          <input class="form-control" type="text" name="partner[]" value="" class="" /> 
         </td> 
         <td> 
          <input class="form-control" type="text" name="director[]" value="" class="" /> 
         </td> 
         <td> 
          <input class="form-control" type="text" name="time[]" value="" class="" /> 
         </td> 
         <td> 
          <textarea class="form-control" name="task[]" rows="2" id=""></textarea> 
         </td> 
         <td> 
          <textarea class="form-control" name="status[]" rows="2" id=""></textarea> 
         </td> 
         <td> 
          <a href="" class="btn btn-danger">Delete</a> 
         </td> 
        </tr> 
        <!-- 
        end : 
        --> 
    <?php 
        } ?> 
    </tbody> 
    </table> 
    <input class="btn btn-primary" type="submit" name="submit" value="submit"> 
    <input class="btn btn-primary" type="submit" name="submit" value="add"> 
    <?php 
    echo form_close(); 
    ?> 
    

    控制器:

    public function update_hodm(){ 
        /* Checking the all validation of task form*/ 
    
        // $this->form_validation->set_rules('date', 'Date', 'required'); 
    
        $this->form_validation->set_rules('work[]', 'Types of Work', 'required'); 
    
        // $this->form_validation->set_rules('partner[]', 'Worked With', 'required'); 
    
        $this->form_validation->set_rules('director[]', 'Director', 'required'); 
        $this->form_validation->set_rules('time[]', 'No Of Hours', 'required'); 
        $this->form_validation->set_rules('task[]', 'Task Details', 'required'); 
        $this->form_validation->set_rules('status[]', 'Task Status', 'required'); 
        if ($this->form_validation->run()) { 
         foreach($_POST['form_id'] as $key => $value) { 
          $data = array(
           'name' => 'aman', 
           'date' => $_POST['date'][$key], 
           'work' => $_POST['work'][$key], 
           'partner' => $_POST['partner'][$key], 
           'director' => $_POST['director'][$key], 
           'time' => $_POST['time'][$key], 
           'task' => $_POST['task'][$key], 
           'status' => $_POST['status'][$key] 
          ); 
          if (!empty($value)) { 
    
           // update 
    
           $this->update->update_hodm($data, $value); 
          } 
          else { 
    
           // insert 
    
           $this->update->insert_hodm($data); 
          } 
         } 
        } 
        else { 
         $this->load->view('public/digital_hodm_view'); 
        } 
    } 
    

    型號:

    public function update_hodm($data,$form_id){ 
         $this->db->where('form_id',$form_id); 
         $this->db->update_batch('task_form', $data,'date');//date is my table column name 
         return true; 
    } 
    
    public function insert_hodm($data){ 
         $this->db->insert('task_form', $data);name 
         return true; 
    } 
    
    +0

    我不完全確定這將完全符合您的要求,因爲我大部分假設然後代碼,但這幾乎是你想要的,因爲我可以從你的問題了解問題,希望它會爲你工作。告訴我,如果額外的幫助需要 –