2017-01-05 5 views
0

Meine erste Frage wurde falsch geschrieben, so dass ich es neu poste. Ich übe mit einem Tutorial auf Tutsplus von Joost van Veen und ich fügte ein Image-Upload auf den Controller, aber jedes Mal, wenn ich versuche, einen Beitrag zu speichern, bekomme ich einen Fehler aus der Datenbank Spruch Spalte "Bild" kann nicht null sein. Ich habe andere Antworten überprüft, aber nichts erklärt das Problem. Jede Hilfe wäre willkommen.Fehler Spalte 'Bild' kann nicht null sein

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 }

Antwort

0

fixierte I, das Problem durch eine neue Methode array_me Erzeugen() in dem Modell, und es in der Steuerung aufgerufen wird.

NEW METHOD IN MY_MODEL 
 

 
public function array_me($fields) { 
 
\t \t \t $uploadData = $this->upload->data(); 
 
\t \t \t $picture = $uploadData['file_name']; 
 
\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 $data['picture'] = $picture; 
 
\t \t \t } 
 
\t \t \t return $data; 
 
\t \t } 
 

 
EDITED 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_me(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 }

Verwandte Themen