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
我不明白。我需要更改 ** $ data = array('upload_data'=> $ this-> upload-> data()); **? – Pankaspe
我已經更新了答案,請檢查它。它可能會幫助你 –
的錯誤是一樣的,返回NULL – Pankaspe