我使用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)然後讓用戶提交更新後的數據併發布到數據庫中?
任何指導或幫助構建我的控制器/視圖文件的其餘部分將不勝感激,因爲我已經在這一天超過一天!
謝謝。