2016-11-28 60 views
0

我要上傳圖片我的新聞模型裏面,但是當我嘗試創建一個新聞條目的CI說:笨3 - 上傳圖片和存儲字符串中的數據庫

INSERT INTO `news` (`title`, `slug`, `text`, `featured_image`) VALUES ('sample title', 'sample-title', 'upload image into db', NULL) 

我在控制器的功能是:

public function create() 
    { 
     $this->load->helper(array('form', 'url')); 
     $this->load->library('form_validation'); 

     // start config image upload 
     $config['upload_path'] = './assets/uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_width'] = 1024; 
     $config['max_height'] = 768; 
     $this->load->library('upload', $config); 
     // end 

     $data['title'] = 'Create a news item'; 

     $this->form_validation->set_rules('title', 'Title', 'required'); 
     $this->form_validation->set_rules('text', 'Text', 'required'); 

     if ($this->form_validation->run() === FALSE) 
     { 
      $this->load->view('templates/header', $data); 
      $this->load->view('news/create'); 
      $this->load->view('templates/footer'); 

     } 
     else 
     { 

      $data = array('upload_data' => $this->upload->data()); 
      $this->news_model->set_news(); 

      $this->load->view('templates/header'); 
      $this->load->view('news/success'); 
      $this->load->view('templates/footer');   
     } 
    } 

而且,我的模型是:

public function set_news($id = 0) 
    { 
     $this->load->helper('url'); 

     $slug = url_title($this->input->post('title'), 'dash', TRUE); 

     $data = array(
      'title' => $this->input->post('title'), 
      'slug' => $slug, 
      'text' => $this->input->post('text'), 
      'featured_image' => $this->input->post('featured_image') 
     ); 

     if ($id == 0) { 
      return $this->db->insert('news', $data); 
     } else { 
      $this->db->where('id', $id); 
      return $this->db->update('news', $data); 
     } 
    } 

而且我認爲的意見/新聞/科瑞e.php

<h2><?php echo $title; ?></h2> 
<hr> 
<?php echo validation_errors(); ?> 

<?php echo form_open_multipart('news/create'); ?> 

    <div class="row"> 
     <div class="six-columns"> 
      <label for="title">Title</label> 
      <input class="u-full-width" type="text" name="title"><br /> 

      <label for="text">Text</label> 
      <textarea class="u-full-width" name="text"></textarea><br /> 

      <label for="image">Featured image</label> 
      <input class="u-full-width" type="file" name="featured_image" /><br /> 

      <input class="button-primary" type="submit" name="submit" value="Create news item" /> 
     </div> 
    </div> 
</form> 

我不能找到一個教程,說明如何將圖片字符串存儲在數據庫中,我的表名新聞和列名是featured_image

回答

0

模型

public function create() 
    { 
     $this->load->helper(array('form', 'url')); 
     $this->load->library('form_validation'); 

     // start config image upload 
     $config['upload_path'] = './assets/uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_width'] = 1024; 
     $config['max_height'] = 768; 
     $this->load->library('upload', $config); 
     // end 

     $data['title'] = 'Create a news item'; 

     $this->form_validation->set_rules('title', 'Title', 'required'); 
     $this->form_validation->set_rules('text', 'Text', 'required'); 

     if ($this->form_validation->run() === FALSE) 
     { 
      $this->load->view('templates/header', $data); 
      $this->load->view('news/create'); 
      $this->load->view('templates/footer'); 

     } 
     else 
     { 

      $image_data= $this->upload->data('featured_image'); 
      $this->load->helper('url'); 

      $slug = url_title($this->input->post('title'), 'dash', TRUE); 

     $data = array(
      'title' => $this->input->post('title'), 
      'slug' => $slug, 
      'text' => $this->input->post('text'), 
      'featured_image' => $image_data['file_name] 
     ); 
      $this->news_model->set_news(0,$data); 

      $this->load->view('templates/header'); 
      $this->load->view('news/success'); 
      $this->load->view('templates/footer');   
     } 
    } 

模型

public function set_news($id = 0,$data) 
    { 
     if ($id == 0) { 
      return $this->db->insert('news', $data); 
     } else { 
      $this->db->where('id', $id); 
      return $this->db->update('news', $data); 
     } 
    } 
+0

我不明白。我需要更改 ** $ data = array('upload_data'=> $ this-> upload-> data()); **? – Pankaspe

+0

我已經更新了答案,請檢查它。它可能會幫助你 –

+0

的錯誤是一樣的,返回NULL – Pankaspe

0

解決,只是把

'featured_image' => $_FILES["featured_image"]["name"] 

到我的模型,我在我的控制器創建一個回調函數(upload_image)

public function image_upload() 
{ 
    $this->load->library('upload'); 

    if (!empty($_FILES['featured_image']['name'])) 
    { 
     // Specify configuration for File 1 
     $config['upload_path'] = './assets/uploads'; 
     $config['allowed_types'] = 'gif|jpg|png'; 

     // Initialize config for File 1 
     $this->upload->initialize($config); 

     // Upload file 1 
     if ($this->upload->do_upload('featured_image')) 
     { 
      $data = $this->upload->data('file_name'); 
      return true; 
     } 
     else 
     { 
      $imageerrors = $this->upload->display_errors(); 
      $this->form_validation->set_message('image_upload', $imageerrors); 
      return false; 
     } 
    } 
} 

,並在我的創造()函數

$this->form_validation->set_rules('featured_image', 'Featured image', 'callback_image_upload');