我正在使用 Codeigniter 框架发展我的技能。我已经查看了数据并将其插入到数据库中,发现更新数据更加棘手。我见过的大多数教程都是在代码中输入值,而不是从数据库中提取选定的 id 并在表单字段中回显。到目前为止,我有:
新闻模型:
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)然后让用户提交更新的数据并发布到数据库中?
对于构建我的其余控制器/视图文件的任何指导或帮助,我将不胜感激,因为我已经在这方面工作了一天多!
谢谢你。