2012-09-06 20 views
1

我使用Codeigniter框架開發自己的技能。我已經查看並將數據插入到數據庫中,並發現更新數據更棘手。我看到的大多數教程都是在代碼中輸入值,而不是從數據庫中提取選定的ID並在表單域中回顯。到目前爲止,我有:Codeigniter更新數據庫:在輸入字段中回顯數據並在提交時更新

news_model:

function editArticle($data) { 
     $data = array(
         'title' => $title, 
         'content' => $content, 
         'author' => $author 
        ); 

     $this->db->where('id', $id); 
     $this->db->update('news', $data, array('id' =>$id)); 

    } 

控制器:

public function update_entry() { 
     //load the upate model 
     $this->load->model('update_model'); 

     //get the article from the database 
     $data['news'] = $this->news_model->get_article($this->uri->segment(4)); 

     // perform validation on the updated article so no errors or blank fields 
     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('title', 'Title', 'trim|required'); 
     $this->form_validation->set_rules('content', 'Content', 'trim|required'); 
     $this->form_validation->set_rules('author', 'Author', 'trim|required'); 

     // If validation fails, return to the edit screen with error messages 
     if($this->form_validation->run() == FALSE) { 

      $this->index(); 

     }else{ 
      //update the news article in the database 
      if($query = $this->update_model->update()) { 

     }else{ 
      redirect('admin/edit'); 
     } 
    } 
} 

查看:

 <?php echo form_open('admin/edit/edit_article'); ?> 

     <?php echo form_input('title', set_value('title', 'Title')); ?><br /> 
     <?php echo form_textarea('content', set_value('content', 'Content')); ?><br /> 
     <?php echo form_input('author', set_value('author', 'Author')); ?> 
     <?php echo form_submit('submit', 'Edit Article'); ?> 
     <?php if (isset($error)){echo "<p class='error'>$error</div>"; 
     }?> 
     <?php echo validation_errors('<p class="error">');?> 
     <?php echo form_close(); ?> 

1)林不知道如何迴應的數據輸出(從當用戶點擊了文章視圖中的編輯按鈕)以獲取該ID,然後顯示在文本字段中的編輯頁面上秒。

2)然後讓用戶提交更新後的數據併發布到數據庫中?

任何指導或幫助構建我的控制器/視圖文件的其餘部分將不勝感激,因爲我已經在這一天超過一天!

謝謝。

回答

0

您還沒有給出所有的代碼爲您的看法,所以我不知道你有多大的權利,有多少你有錯,但我會提一些事情,我可以看到 -

您似乎沒有從您的控制器調用您的視圖,如$this->load->view('edit', $data);(請參閱http://codeigniter.com/user_guide/general/views.html),其中當前字段的內容位於$data

要預填充表單字段,請將當前字段值置於set_value()的第二個參數中,例如set_value('title', $article->title)之類的內容。

您的模型還需要處理提交的表單(在$this->input->post中),然後在模型中調用更新查詢。

(我不得不說的是,CodeIgniter的文檔是不是很大就這一點 - 你必須要尋找在Form HelperForm Validation Class文檔,以及保持意見的跟蹤(以上鍊接),ControllersModels(加上一個或兩個人,我不應該懷疑))。

相關問題