Php Codeigniter更新数据库:在输入字段中回显数据并在提交时更新
我正在使用Codeigniter框架开发我的技能。我已经查看了数据并将其插入数据库,发现更新数据更加困难。我看到的大多数教程都是在代码中输入值,而不是从数据库中提取一个选定的id并在表单字段中回显。到目前为止,我已经: 新闻组模型:Php Codeigniter更新数据库:在输入字段中回显数据并在提交时更新,php,codeigniter,activerecord,updatemodel,Php,Codeigniter,Activerecord,Updatemodel,我正在使用Codeigniter框架开发我的技能。我已经查看了数据并将其插入数据库,发现更新数据更加困难。我看到的大多数教程都是在代码中输入值,而不是从数据库中提取一个选定的id并在表单字段中回显。到目前为止,我已经: 新闻组模型: function editArticle($data) { $data = array( 'title' => $title, 'content' =&
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');
}
}
}
视图:
1) 我不知道如何回显数据(当用户从显示的文章视图中单击编辑按钮时),以获取该id,然后显示在编辑页面的文本字段中
2) 然后让用户提交更新的数据并在数据库中发布
如果您能为我的控制器/视图文件的其余部分提供任何指导或帮助,我们将不胜感激,因为我已经在这方面做了一天多了
谢谢。您还没有为您的视图提供所有代码,因此我不确定您有多少是对的,有多少是错的,但我会提到一些我可以看到的东西- 您似乎没有像
$this->load->view('edit',$data)那样从控制器调用视图代码>(请参阅)当前字段的内容位于$data
中
要预填充表单字段,请将当前字段值放入set\u value()
的第二个参数中,例如set\u value('title',$article->title)
您的模型还需要处理提交的表单(在$this->input->post
中),然后在模型中调用更新查询
(我不得不说CodeIgniter文档在这方面不是很好——你必须查看文档和文档,并跟踪视图(上面的链接),以及(加上一两个其他的,我不应该感到奇怪))
<?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(); ?>