【问题标题】:Codeigniter Update database: echo data back in input fields and update on submitCodeigniter 更新数据库:在输入字段中回显数据并在提交时更新
【发布时间】:2012-08-31 08:50:32
【问题描述】:

我正在使用 Codeigniter 框架发展我的技能。我已经查看了数据并将其插入到数据库中,发现更新数据更加棘手。我见过的大多数教程都是在代码中输入值,而不是从数据库中提取选定的 id 并在表单字段中回显。到目前为止,我有:

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) 然后让用户提交更新后的数据并发布到数据库中?

任何关于构建我的其余控制器/视图文件的指导或帮助将不胜感激,因为我已经在这方面工作了一天多!

谢谢。

【问题讨论】:

    标签: php codeigniter activerecord updatemodel


    【解决方案1】:

    你还没有给出你的视图的所有代码,所以我不确定你有多少是正确的,你有多少是错误的,但我会提到一些我能看到的东西 -

    您似乎没有像 $this-&gt;load-&gt;view('edit', $data); 那样从控制器调用您的视图(参见 http://codeigniter.com/user_guide/general/views.html),其中当前字段的内容位于 $data 中。

    要预填充表单字段,请将当前字段值放入set_value() 的第二个参数中,例如set_value('title', $article-&gt;title)

    你的模型还需要处理提交的表单($this-&gt;input-&gt;post),然后在模型中调用更新查询。

    (我不得不说 CodeIgniter 文档在这方面并不出色 - 您必须查看 Form HelperForm Validation Class 文档,以及跟踪视图(上面的链接)、ControllersModels(加上一两个其他人,我不应该怀疑)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多