dongyou2305 2018-04-27 19: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 20: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()

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。