0
我的第一個問題發佈錯了,所以我重新發布它。我正在練習一個關於tutsplus的教程,由joost van veen完成,我添加了一個圖像上傳到控制器,但是每次我嘗試保存一篇文章時,我都會從數據庫中得到一個錯誤,說'圖片'欄不能爲空。我查了其他答案,但沒有任何解釋的問題。任何幫助,將不勝感激。錯誤欄'圖片'不能爲空
MY CONTROLLER
public function edit($post_id = NULL) {
\t \t // Fetch all articles or set a new one
\t \t if ($post_id) {
\t \t \t $this->data['article'] = $this->article_m->get($post_id);
\t \t \t count($this->data['article']) || $this->data['errors'][] = 'article could not be found';
\t \t }
\t \t else {
\t \t \t $this->data['article'] = $this->article_m->get_new();
\t \t }
\t \t // Set up the form
\t \t $rules = $this->article_m->rules;
\t \t $this->form_validation->set_rules($rules);
\t \t if ($this->input->post('userSubmit')) {
\t \t \t //check if user uploads picture
\t \t \t if (!empty($_FILES['picture']['name'])) {
\t \t \t \t $config['upload_path'] = './uploads/';
\t \t \t \t $config['allowed_types'] = 'jpg|png|gif|jpeg';
\t \t \t \t $config['file_name'] = $_FILES['picture']['name'];
\t \t \t \t //load upload library and initialize configuration
\t \t \t \t
\t \t \t \t $this->upload->initialize($config);
\t \t \t \t if ($this->upload->do_upload('picture')) {
\t \t \t \t \t $uploadData = $this->upload->data();
\t \t \t \t \t $picture = $uploadData['file_name'];
\t \t \t \t } else {
\t \t \t \t \t $picture = '';
\t \t \t \t }
\t \t \t } else {
\t \t \t \t $picture = '';
\t \t \t }
\t \t \t // prepare array of posts data
\t \t \t $data = $this->article_m->array_from_post(array(
\t \t \t \t 'title',
\t \t \t \t 'slug',
\t \t \t \t 'content',
\t \t \t \t 'category_id',
\t \t \t \t 'picture',
\t \t \t \t 'pubdate'
\t \t \t));
\t \t \t $insertPosts = $this->article_m->save($data, $post_id);
\t \t \t redirect('admin/article');
\t \t \t //storing insertion status message
\t \t \t if ($insertPosts) {
\t \t \t \t $this->session->set_flashdata('success_msg', 'Post has been added Successfully.');
\t \t \t } else {
\t \t \t \t $this->session->set_flashdata('error_msg', 'error occured while trying upload, please try again.');
\t \t \t }
\t \t }
\t \t // Load view
\t \t $this->data['subview'] = 'admin/article/edit';
\t \t $this->load->view('admin/components/page_head', $this->data);
\t \t $this->load->view('admin/_layout_main', $this->data);
\t \t $this->load->view('admin/components/page_tail');
\t }
MY_MODEL
public function array_from_post($fields) {
\t \t \t $data = array();
\t \t \t foreach ($fields as $field) {
\t \t \t \t $data[$field] = $this->input->post($field);
\t \t \t \t $data['category_id'] = $this->input->post('category');
\t \t \t }
\t \t \t return $data;
\t \t }
public function save($data, $id = NULL) {
\t \t \t // Set timestamps
\t \t \t if ($this->_timestamps == TRUE) {
\t \t \t \t $now = date('Y-m-d H:i:s');
\t \t \t \t $id || $data['created'] = $now;
\t \t \t \t $data['modified'] = $now;
\t \t \t }
\t \t \t // Insert
\t \t \t if ($id === NULL) {
\t \t \t \t !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
\t \t \t \t $this->db->set($data);
\t \t \t \t $this->db->insert($this->_table_name);
\t \t \t \t $id = $this->db->insert_id();
\t \t \t }
\t \t \t // Update
\t \t \t else {
\t \t \t \t $filter = $this->_primary_filter;
\t \t \t \t $id = $filter($id);
\t \t \t \t $this->db->set($data);
\t \t \t \t $this->db->where($this->_primary_key, $id);
\t \t \t \t $this->db->update($this->_table_name);
\t \t \t }
\t \t \t return $id;
\t \t }
RULES TO SET FORM VALIDATION
public $rules = array(
\t \t \t 'pubdate' => array(
\t \t \t \t 'field' => 'pubdate',
\t \t \t \t 'label' => 'Publication date',
\t \t \t \t 'rules' => 'trim|required|exact_length[10]'
\t \t \t),
\t \t \t 'title' => array(
\t \t \t \t 'field' => 'title',
\t \t \t \t 'label' => 'Title',
\t \t \t \t 'rules' => 'trim|required|max_length[100]'
\t \t \t),
\t \t \t 'slug' => array(
\t \t \t \t 'field' => 'slug',
\t \t \t \t 'label' => 'Slug',
\t \t \t \t 'rules' => 'trim|required|max_length[100]|url_title'
\t \t \t),
\t \t \t 'content' => array(
\t \t \t \t 'field' => 'content',
\t \t \t \t 'label' => 'Content',
\t \t \t \t 'rules' => 'trim|required'
\t \t \t),
\t \t \t 'picture' => array(
\t \t \t \t 'field' => 'picture',
\t \t \t \t 'label' => 'Upload File',
\t \t \t \t 'rules' => 'trim'
\t \t \t),
\t \t);
\t \t public function get_new() {
\t \t \t $article = new stdClass();
\t \t \t $article->title = '';
\t \t \t $article->category_id = '';
\t \t \t $article->slug = '';
\t \t \t $article->content = '';
\t \t \t $article->picture = '';
\t \t \t $article->pubdate = date('Y-m-d');
\t \t \t return $article;
\t \t }