上传文件路径到数据库并保存文章Codeigniter不工作

时间:2013-11-30 06:33:23

标签: php codeigniter

我在Codeigniter 2.1.4上开发了一个简单的cms,我有一个用于创建文章的部分。在创建文章表单中,我有一些标题,slug,作者等的基本输入以及用于上传图像文件的输入。 大约一个月前,当我为创建文章页面创建控制器和模型时,一切都很顺利,但现在它不会工作。 问题是我无法将文章保存到数据库,它只是将我重定向到文章列表页面,没有错误就像一切都很好。

这是我的控制者:

<?php

class Articole extends Admin_Controller {
    var $portofoliu_path;
    var $portofoliu_path_thumb;

    public function __construct() {
        parent::__construct ();
        $this->load->model ('article_m');
        $this->load->model ('photo_m');

        $this->portofoliu_path = './fisiere/imagini/articole/';
        $this->portofoliu_path_thumb = './fisiere/imagini/articole/';
    }

    public function index() {
        // Fetch all articles
        $this->data['articles'] = $this->article_m->get ();

        // Load view
        $this->load->view ('admin/articole/index', $this->data);
    }

    public function editeaza($id = NULL) {

        // Fetch a article or set a new one
        if ($id) {
            $this->data['article'] = $this->article_m->get ($id);
            count ($this->data['article']) || $this->data['errors'][] = 'article could not be found';
        } else {
            $this->data['article'] = $this->article_m->get_new ();
        }

        // Set up the form
        $rules = $this->article_m->rules;
        $this->form_validation->set_rules ($rules);

        // Process the form
        if ($this->form_validation->run () == TRUE) {
            $data = $this->article_m->array_from_post (array (
                    'title',
                    'slug',
                    'body',
                    'pubdate',
                    'categoria',
                    'author',
                    'keywords',
                    'description',
                    'observatii' 
            ));

            $config = array (
                    'allowed_types' => 'jpg|jpeg|gif|png',
                    'upload_path' => $this->portofoliu_path,
                    'max_size' => 100000,
                    'remove_spaces' => true 
            );

            $this->load->library ('upload', $config);
            $this->upload->do_upload ();

            $image_data = $this->upload->data ();

            $config = array (
                    'source_image' => $image_data['file_path'],
                    'new_image' => $this->portofoliu_path . 'thumbnails/',
                    'maintain_ratio' => true,
                    'width' => 150,
                    'height' => 150 
            );


            $this->load->library ('image_lib', $config);
            $this->image_lib->fit ();

            $temp = $this->upload->data ();
            $image = $temp['file_name']; // to get image file name rom upload script , as it could be stored in the databae


            $data = array (
                    'userfile' => $image,
                    'imgalt' => $this->input->post ('imgalt') 
            );

            $this->photo_m->do_upload ($data, $id);
            $this->article_m->save ($data, $id);
            redirect ('admin/articole');
        }

        // Load the view
        $this->load->view ('admin/articole/editeaza', $this->data);
    }

    public function sterge($id) {
        $this->article_m->delete ($id);
        redirect ('admin/articole');
    }
}

这是文章模型:

<?php
class Article_m extends MY_Model
{
    protected $_table_name = 'articles';
    protected $_order_by = 'pubdate desc, id desc';
    protected $_timestamps = TRUE;
    public $rules = array(
        'pubdate' => array(
            'field' => 'pubdate', 
            'label' => 'Publication date', 
            'rules' => 'trim|required|exact_length[10]|xss_clean'
            ), 
        'title' => array(
            'field' => 'title', 
            'label' => 'Title', 
            'rules' => 'trim|required|max_length[100]|xss_clean'
            ), 
        'slug' => array(
            'field' => 'slug', 
            'label' => 'Slug', 
            'rules' => 'trim|required|max_length[100]|url_title|xss_clean'
            ), 
        'body' => array(
            'field' => 'body', 
            'label' => 'Body', 
            'rules' => 'trim|required'
            ),
        'categoria' => array(
            'field' => 'categoria', 
            'label' => 'Categoria', 
            'rules' => 'trim|required'
            ),
        'author' => array(
            'field' => 'author', 
            'label' => 'Categoria', 
            'rules' => 'trim'
            ),
        'userfile' => array(
            'field' => 'userfile', 
            'label' => 'Imagine', 
            'rules' => 'trim'
            ),          
        'imgalt' => array(
            'field' => 'imgalt', 
            'label' => 'Text Alternativ', 
            'rules' => 'trim'
            ),
            'keywords' => array(
                'field' => 'keywords', 
                'label' => 'Cuvinte Cheie', 
                'rules' => 'trim'
                ),
            'description' => array(
                'field' => 'description', 
                'label' => 'Descriere', 
                'rules' => 'trim'
                ),
            'observatii' => array(
                'field' => 'observatii', 
                'label' => 'Observații', 
                'rules' => 'trim'
                )
            );

public function get_new ()
{
    $article = new stdClass();
    $article->title = '';
    $article->slug = '';
    $article->body = '';
    $article->categoria = '';
    $article->author = '';
    $article->userfile = '';
    $article->imgalt = '';
    $article->keywords = '';
    $article->description = '';
    $article->observatii = '';
    $article->pubdate = date('Y-m-d');
    return $article;
}

public function set_published(){
    $this->db->where('pubdate <=', date('Y-m-d'));
}

public function get_recent($limit = 3){

            // Fetch a limited number of recent articole
    $limit = (int) $limit;
    $this->set_published();
    $this->db->limit($limit);
    return parent::get();
}

}

这是上传到文件夹和db:

的照片模型
<?php

class Photo_m extends MY_Model {

  protected $_table_name = 'articles';
  protected $_order_by = 'id';
  public $rules = array(
      'userfile' => array(
          'field' => 'userfile', 
          'label' => 'Imagine', 
          'rules' => 'trim'
      ), 
      'imgalt' => array(
          'field' => 'imgalt', 
          'label' => 'Descriere', 
          'rules' => 'trim'
      )
  );


  public function do_upload($data, $id = NULL) {


      if($data['userfile'] != '' && $id === NULL) {

      !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
      $this->db->set($data);
      $this->db->insert($this->_table_name);
      $id = $this->db->insert_id();

      }

    else if ($data['userfile'] != '') {
      $filter = $this->_primary_filter;
      $id = $filter($id);
      $this->db->set($data);
      $this->db->where($this->_primary_key, $id);
      $this->db->update($this->_table_name);
    }

    else {
      redirect('admin/articole');
      }
  }

}

1 个答案:

答案 0 :(得分:0)

在更新时遇到了问题,但是...我设法让它工作,现在我的控制器看起来像这样:

<?php

class Articole extends Admin_Controller {
    var $portofoliu_path;
    var $portofoliu_path_thumb;

    public function __construct() {
        parent::__construct ();
        $this->load->model ('article_m');
        $this->load->model ('photo_m');

        $this->portofoliu_path = './fisiere/imagini/articole/' . $id;
        $this->portofoliu_path_thumb = './fisiere/imagini/articole/' . $id;
    }

    public function index() {
        // Fetch all articles
        $this->data['articles'] = $this->article_m->get ();

        // Load view
        $this->load->view ('admin/articole/index', $this->data);
    }

    public function editeaza($id = NULL) {

        // Fetch a article or set a new one
        if ($id) {
            $this->data['article'] = $this->article_m->get ($id);
            count ($this->data['article']) || $this->data['errors'][] = 'article could not be found';
        } else {
            $this->data['article'] = $this->article_m->get_new ();
        }


                    $config = array (
                    'allowed_types' => 'jpg|jpeg|gif|png',
                    'upload_path' => $this->portofoliu_path,
                    'max_size' => 100000,
                    'remove_spaces' => true 
            );

            $this->load->library ('upload', $config);
            $this->upload->do_upload ();

            $image_data = $this->upload->data ();

            $config = array (
                    'source_image' => $image_data['file_path'],
                    'new_image' => $this->portofoliu_path . 'thumbnails/',
                    'maintain_ratio' => true,
                    'width' => 150,
                    'height' => 150 
            );

            // $data = array (
            //      'userfile' => $image,
            //      'imgalt' => $this->input->post ('imgalt') 
            // );

            $this->load->library ('image_lib', $config);
            $this->image_lib->fit ();

            $temp = $this->upload->data ();
            $image = $temp['file_name']; // to get image file name rom upload script , as it could be stored in the databae

            $data = array (
                    'userfile' => $image,
                    'imgalt' => $this->input->post ('imgalt') 
            );

            $this->photo_m->do_upload ($data, $id);

        // Set up the form
        $rules = $this->article_m->rules;
        $this->form_validation->set_rules ($rules);

        // Process the form
        if ($this->form_validation->run () == TRUE) {
            $data = $this->article_m->array_from_post (array (
                    'title',
                    'slug',
                    'body',
                    'pubdate',
                    'categoria',
                    'keywords',
                    'description',
                    'observatii' 
            ));         


            $this->article_m->save ($data, $id);
            //print_r ($this->db->last_query()); exit();
            redirect ('admin/articole');
        }

        // Load the view
        $this->load->view ('admin/articole/editeaza', $this->data);
    }

    public function sterge($id) {
        $this->article_m->delete ($id);
        redirect ('admin/articole');
    }
}

TeşekkürlerNaveedHasan的帮助!!!