我已經在CodeIgniter3框架中編寫了一些代碼來編輯和更新帖子。但是當我點擊提交按鈕它重定向到404頁面,並顯示沒有像這裏這樣的頁面我試圖在控制器中創建$數據,然後將其移到模型它不起作用,現在我不知道在哪裏問題。這是我的觀點:CODEIGNITER3框架在編輯帖子時顯示頁面不存在
<?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);
}
如果不使用路由,您呼叫控制器'courses',然後調用funvtion更新 – Vickel
@Vickel一個未知的網頁,謝謝!我忘了添加路線:D但我有一個問題。我是編程新手。這些路線是做什麼的?爲什麼在添加路線之前它不起作用? –
CI路由在這裏有很好的記錄:https://www.codeigniter.com/user_guide/general/routing.html沒有路由,你會按照這樣的CI URL方法:https://www.codeigniter.com/user_guide/ general/urls.html,這意味着(如你的截圖所示),你可以在控制器'courses'中調用函數'update'。在CI主頁上閱讀教程,您將學到很多東西! – Vickel