Codeigniter(将类别数据插入表中)

时间:2018-12-04 09:17:03

标签: php codeigniter

我是CodeIgniter的新手,我进行了一个由前开发人员离开的电子商务项目。情况是类别数据没有插入到我的表中。

控制器和模型中的代码都非常长,但是我剪掉了它,只张贴了必要的部分。

这是我的控制器。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends Admin_Controller {
public function create()
    {
        /* Breadcrumbs */
        $this->breadcrumbs->unshift(2, "New Category" , 'admin/category/create');
        $this->data['breadcrumb'] = $this->breadcrumbs->show();

        /* Variables */
        $tables = $this->config->item('tables', 'ion_auth');

        /* Validate form input */
        $this->form_validation->set_rules('cat_name', 'Category Name', 'trim|required');       



        if ($this->form_validation->run() == TRUE)
        {

            $config['upload_path'] =  './assets/uploads/category/';
            //die(var_dump(is_dir($config['upload_path'])));
            $config['allowed_types'] = 'png,jpeg';
            $config['max_size']    = '1024';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $img = "icon";
            if ( ! $this->upload->do_upload($img))
            {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('admin/category');
            }
            else
            {
                $data=$this->upload->data();
                $file = array('file_name' => $data['file_name'] );

                $data = array('upload_data' => $this->upload->data());
                $photo = base_url().'assets/uploads/category/'.$file['file_name'];

                $data = array(
                    'category_name' => $this->input->post('cat_name'),
                    'category_photo'  => $photo,
                    'category_description'    => $this->input->post('cat_desc')
                );

                $this->category_model->insertcategory($data);
                //$this->ion_auth->messages()
                $this->session->set_flashdata('message', "Successfully inserted!");
                redirect('admin/category', 'refresh');
            }

        }
        else
        {
            $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));           

            /* Load Template */
            $this->template->admin_render('admin/category/create', $this->data);
        }
    }

这是我的模特。

class Category_model extends CI_Model
{
function insertcategory($data) {

        $query = $this->db->insert('category', $data);

        if ($query) {
            return true;
        } else {
            return false;
        }
    }

这是我的表格。

<div class="box-body">
                                    <span style="color:red"><?php echo $message;?></span>

                                    <?php echo form_open_multipart(current_url(), array('class' => 'form-horizontal', 'id' => 'form-create_user')); ?>
                                        <div class="form-group">
                                            <span class="col-sm-2 control-label">Category Name</span> 
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="cat_name" placeholder="Category Name" name="cat_name" required>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <span class="col-sm-2 control-label">Category Description</span> 
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="cat_desc" placeholder="Description" name="cat_desc" >
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <span class="col-sm-2 control-label">Category Icon</span> 
                                            <div class="col-sm-10">
                                                <input class="input-file uniform_on" id="icon" name="icon" type="file">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-sm-offset-2 col-sm-10">
                                                <div class="btn-group">
                                                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-primary btn-flat', 'content' => lang('actions_submit'))); ?>
                                                    <?php echo form_button(array('type' => 'reset', 'class' => 'btn btn-warning btn-flat', 'content' => lang('actions_reset'))); ?>
                                                    <?php echo anchor('admin/category', lang('actions_cancel'), array('class' => 'btn btn-default btn-flat')); ?>
                                                </div>
                                            </div>
                                        </div>
                                    <?php echo form_close();?>
                                </div>

1 个答案:

答案 0 :(得分:2)

您能否将 $ img =“ icon”; 替换为 $ img = $ this-> input-> post('icon');

请检查以上数据。

还请发布您收到的错误消息。