CODEIGNITER3框架显示编辑帖子时页面不存在

时间:2017-09-16 15:25:16

标签: php codeigniter

我在CodeIgniter3框架中编写了一些代码来编辑和更新帖子。但是,当我点击提交按钮时,它会重定向到404页面并显示没有像这样的页面enter image description here我试图在控制器中创建$ data,然后将其移动到模型它不起作用,现在我不知道问题在哪里。以下是我的观点:

<?php $attributes = array('class' => 'rex-forms', 'name' => 'editcourse', 'id' => 'editcourse', 'data-parsley-validate' => ''); ?>
      <?php echo form_open_multipart('courses/update', $attributes); ?>
      <input type="hidden" name="courses_slug" value="<?= $courses['courses_slug'];?>">

    <!--  General -->

    HERE IS SOME CODE

</form>

这是我的控制器:

public function edit($courses_slug) {

        //check login
         if(!$this->session->userdata('logged_in')) {
            redirect('');
         }

        $data['courses'] = $this->popular_courses_model->get_popular_courses($courses_slug);

        //check user

        if($this->session->userdata('id') != $data['courses']['instructor_id']){
            redirect('');
        }

        $data['categories'] = $this->courses_model->get_all_categories_for_courses();
        $data['title'] = 'Edit Course';

        if (empty($data['courses'])) {
            show_404();
        }

        $this->load->view('templates/header');
        $this->load->view('courses/editcourse', $data);
        $this->load->view('templates/footer');
    }

    public function update() {
        //check login
         if(!$this->session->userdata('logged_in')) {
            redirect('');
         }

        $this->courses_model->update_post();

        $this->session->set_flashdata('post_updated', 'Your post has been updated');
        redirect('blog');
    }

这是我的模特:

public function update_post() {
        //insert image
            $config['upload_path']   = './assets/img/single_courses/';
            $config['allowed_types'] = 'jpg|png';
            $config['max_size']      = 4096;
            $config['overwrite']     = FALSE;
            $config['remove_spaces'] = TRUE;

            $this->load->library('upload', $config);


            if(!$this->upload->do_upload('userfile')){

                $post_image =  '';
            } else {
                $datar = array('upload_data' => $this->upload->data());
                $post_image = $_FILES['userfile']['name'];
            }  

            //insert the user registration details into database
            $slugtitle = mb_strtolower($this->input->post('name'), 'UTF-8');
            $slug = url_title($slugtitle);

            $data = array(
                'title' => strip_tags($this->input->post('name')),
                'duration' => $this->input->post('duration'),
                'certification' => $this->input->post('certification'),
                'price' => strip_tags($this->input->post('price')),
                'begining' => $this->input->post('begining'),
                'courses_description' => $this->input->post('description'),
                'purpose' => $this->input->post('purpose'),
                'courses_slug' => $slug,
                'instructor_id' => $this->session->userdata('id'),
                'category_id' => $this->input->post('category'),
                'img' => $post_image
            );

        $this->db->where('courses_slug', $this->input->post('courses_slug'));
        return $this->db->update('courses', $data);
    }

1 个答案:

答案 0 :(得分:0)

您使用的是模型popular_courses_model,但是您已将模型加载到自动加载中,因为您尚未将模型加载到控制器中。

$this->load->model('popular_courses_model');

您的模型缺少某些构造,除非您刚刚在模型中发布了此功能。假设你没有,你的模型需要看起来像这样:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Popular_courses_model extends CI_Model
{     
    public function __construct()
    {
        parent::__construct();
        // Whatever code you want in your constructor
    }

    public function update_post()
    {
        // your code here
    }    
}

你的模型将是Popular_courses_model.php,大写字母和你的&#39;模型&#39;文件夹中。

希望有所帮助,