dongyou2305 2018-04-27 11:46
浏览 31
已采纳

Codeigniter 3博客应用程序:重定向到更新的帖子失败

I am working on a basic (just 2 tables: authors and posts) blog application in Codeigniter 3.1.8.

I have an edit post functionality, using an update form:

<?php echo form_open("posts/update"); ?>
      <input type="hidden" name="id" id="pid" value="<?php echo $post->id; ?>">
      <div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
        <input type="text" name="title" id="title" class="form-control" placeholder="Title" value="<?php echo $post->title; ?>">
        <?php if(form_error('title')) echo form_error('title'); ?> 
      </div>
      <div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
        <input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription" value="<?php echo $post->description; ?>">
        <?php if(form_error('desc')) echo form_error('desc'); ?> 
      </div>
      <div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
        <textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"><?php echo $post->content; ?></textarea>
        <?php if(form_error('body')) echo form_error('body'); ?> 
      </div>
      <div class="form-group">
        <input type="submit" value="Save" class="btn btn-block btn-md btn-success">
      </div>
<?php echo form_close(); ?>

In the Posts_model model I have the method responsible with updating the post:

public function update_post() {
    $data = [
        'title' => $this->input->post('title'),
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body')
    ];
    $this->db->where('id', $this->input->post('id'));
    return $this->db->update('posts', $data);
}

In the Posts controller, I have 2 methods, for editing and updating the post:

public function edit($id) {
    $data = $this->Static_model->get_static_data();
    $data['post'] = $this->Posts_model->get_post($id);
    $data['tagline'] = 'Edit the post "' . $data['post']->title . '"';
    $this->load->view('partials/header', $data);
    $this->load->view('edit');
    $this->load->view('partials/footer');
}

public function update() {
    $this->Posts_model->update_post();
    // Redirect to the updated post

}

In my update() method, I was unable to redirect to the updated post. The redirect($this->agent->referrer()); line only brings me back to the update form. Where I want to redirect to is the just updated post. How do I do that?

展开全部

  • 写回答

1条回答 默认 最新

  • drddx3115 2018-04-27 12:46
    关注

    Post vars should be handled in your controller:

    public function update() {
        $id = $this->input->post('id');
        $data = [
            'title' => $this->input->post('title'),
            'description' => $this->input->post('desc'),
            'content' => $this->input->post('body')
        ];
        // Update post
        $this->Posts_model->update_post($id, $data);
        // Redirect to the updated post
        redirect('posts/post/' . $id);
    }
    

    Model:

    public function update_post($id, $data) {
        $this->db->where('id', $id);
        return $this->db->update('posts', $data);
    }
    

    Note: if you want to do it your way, then simply return the id in the model function and redirect based on the return of the function in update()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部