Php 正在将文件路径上载到数据库并保存文章Codeigniter未工作

Php 正在将文件路径上载到数据库并保存文章Codeigniter未工作,php,codeigniter,Php,Codeigniter,我有一个在Codeigniter 2.1.4上开发的简单cms,我有一个创建文章的部分。在createarticle表单中,我有一些关于标题、slug、author等的基本输入,还有一个用于上传图像文件的输入。 大约一个月前,当我为“创建文章”页面创建控制器和模型时,一切都进行得非常顺利,但现在它无法工作。 问题是我无法将文章保存到db,它只是将我重定向到文章列表页面,没有任何错误,就像一切正常一样 这是我的控制器: <?php class Articole extends Admin_

我有一个在Codeigniter 2.1.4上开发的简单cms,我有一个创建文章的部分。在createarticle表单中,我有一些关于标题、slug、author等的基本输入,还有一个用于上传图像文件的输入。 大约一个月前,当我为“创建文章”页面创建控制器和模型时,一切都进行得非常顺利,但现在它无法工作。 问题是我无法将文章保存到db,它只是将我重定向到文章列表页面,没有任何错误,就像一切正常一样

这是我的控制器:

<?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 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');
    }
}

我为我的错误道歉,但英语不是我的母语。我很难一下子看清楚整件事,但我没有看到一个在控制器中插入数据的函数,只有一个编辑它的函数:editeaza()。如果这也是为插入调用的,请尝试通过在$this->article\m->save($data,$id)之后的editeaza()函数中添加此行进行调试,添加:echo$this->db->last\u query();退出();我使用了print\r,我得到了这样的结果:UPDATE
articles
SET
userfile
='',
imgalt
='test',
modified
='2013-11-30 09:53:49',其中
id
=1ok,这是一个更新语句,您说插入新文章有问题,对吗。您在更新它们时没有问题。我说的对吗?它现在起作用了…看看我的答案。
<?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');
      }
  }

}
<?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');
    }
}