数据不会保存在数据库中

时间:2015-08-21 15:38:07

标签: php mysql codeigniter

有一个带有图像上传功能的表单,并且有一个外键和自动增量主键。当我提交包含所有数据的表单时,它只在数据库中保存图像的路径。我找不到它的原因。请帮帮我。

这是我的观点:

<?php echo form_open_multipart('floor_plan_controller/save')  ?>

<table class="table">
    <tr> 
        <td>Title</td>
        <td ><?php echo form_input('title') ; ?></td>
    </tr>

    <tr>
     <td>Client</td>
    <td><?php
       $attributes = 'class = "form-control" id = "user"';
        echo form_dropdown('user',$user, set_value('user'), $attributes);?>
    </td>
</tr>


<tr>
    <td>Designed by</td>

    <td><?php
       $attributes = 'class = "form-control" id = "staff"';
        echo form_dropdown('staff',$staff, set_value('staff'), $attributes);?>
    </td>
</tr>

    <tr> 
        <td>Floor Plan</td>
        <td><?php echo form_upload('pic') ; ?></td>
    </tr>

    <tr> 
        <td></td>
        <td><?php echo form_submit('submit', 'save', 'class="btn btn-primary"') ; ?></td>
    </tr>
</table>

型号:

class floor_plan_model extends CI_Model{

    //Get client name
    function get_user()     
    { 
        $this->db->select('id');
        $this->db->select('firstname');
        $this->db->from('user');
        $query = $this->db->get();
        $result = $query->result();

        $user_id = array('-SELECT-');
        $firstname = array('-SELECT-');

        for ($i = 0; $i < count($result); $i++)
        {
            array_push($user_id, $result[$i]->id);
            array_push($firstname, $result[$i]->firstname);
        }
        return $user_result = array_combine($user_id, $firstname);
    }


//    get staff
    function get_staff()     
    { 
        $this->db->select('id');
        $this->db->select('first_name');
        $this->db->from('staff');
        $query = $this->db->get();
        $result = $query->result();

        $staff_id = array('-SELECT-');
        $first_name = array('-SELECT-');


        for ($i = 0; $i < count($result); $i++)
        {
            array_push($staff_id, $result[$i]->id);
            array_push($first_name, $result[$i]->first_name);

        }
        return $staff_result = array_combine($staff_id, $first_name);
    }



public function save($title, $url){

    $this->db->set('title',$title);
    $this->db->set('image',$url);
    $this->db->insert('floor_plan');

}

}

控制器:

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

class Floor_plan_controller extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('floor_plan_model');
        $this->load->library('form_validation');
    }


   public function index() {

        $data['user'] = $this-> floor_plan_model ->get_user();
        $data['staff'] = $this-> floor_plan_model ->get_staff();

       $this->load->view('admin_include/header');
       $this->load->view('plan/floor_plan_add', $data);

   }

   public function save(){
       $url = $this->do_upload();
       $title = $_POST['title'];
       $this-> floor_plan_model->save($title, $url);

   }

   private function do_upload(){
       $type = explode('.', $_FILES["pic"]["name"]);
       $type = $type[count($type)-1];
       $url = "./uploads/plan/".uniqid(rand()).'.'.$type;

       if(in_array($type, array('jpg','jpeg','gif','png')))
        if(is_uploaded_file($_FILES["pic"]['tmp_name']))
            if(move_uploaded_file(($_FILES["pic"]['tmp_name']), $url))
              return $url;
            return "";
   }
}

1 个答案:

答案 0 :(得分:0)

查看

<?php echo form_open_multipart('floor_plan_controller/save')  ?>

<table class="table">
    <tr> 
        <td>Title</td>
        <td ><?php echo form_input('title') ; ?></td>
    </tr>

    <tr>
     <td>Client</td>
    <td><?php
       $attributes = 'class = "form-control" id = "user"';
        echo form_dropdown('user',$user, set_value('user'), $attributes);?>
    </td>
</tr>


<tr>
    <td>Designed by</td>

    <td><?php
       $attributes = 'class = "form-control" id = "staff"';
        echo form_dropdown('staff',$staff, set_value('staff'), $attributes);?>
    </td>
</tr>

    <tr> 
        <td>Floor Plan</td>
        <td><?php echo form_upload('pic') ; ?></td>
    </tr>

    <tr> 
        <td></td>
        <td><?php echo form_submit('submit', 'save', 'class="btn btn-primary"') ; ?></td>
    </tr>
</table>

<强>控制器

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

class Floor_plan_controller extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('floor_plan_model');
        $this->load->library('form_validation');
    }


   public function index() {

        $data['user'] = $this-> floor_plan_model ->get_user();
        $data['staff'] = $this-> floor_plan_model ->get_staff();

       $this->load->view('admin_include/header');
       $this->load->view('plan/floor_plan_add', $data);

   }

//   save image and other data
   public function save(){
       $url = $this->do_upload();
       $title = $_POST['title'];
       $user_id = $_POST['user'];
       $staff_id = $_POST['staff'];
       $this-> floor_plan_model->save($title, $url, $user_id, $staff_id);

   }

//   upload image
   private function do_upload(){
       $type = explode('.', $_FILES["pic"]["name"]);
       $type = $type[count($type)-1];
       $url = "./uploads/plan/".uniqid(rand()).'.'.$type;

       if(in_array($type, array('jpg','jpeg','gif','png')))
        if(is_uploaded_file($_FILES["pic"]['tmp_name']))
            if(move_uploaded_file(($_FILES["pic"]['tmp_name']), $url))
              return $url;
            return "";
   }
}

<强>模型

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

class floor_plan_model extends CI_Model{

    //Get client name
    function get_user()     
    { 
        $this->db->select('id');
        $this->db->select('firstname');
        $this->db->from('user');
        $query = $this->db->get();
        $result = $query->result();

        $user_id = array('-SELECT-');
        $firstname = array('-SELECT-');

        for ($i = 0; $i < count($result); $i++)
        {
            array_push($user_id, $result[$i]->id);
            array_push($firstname, $result[$i]->firstname);
        }
        return $user_result = array_combine($user_id, $firstname);
    }


//    get staff
    function get_staff()     
    { 
        $this->db->select('id');
        $this->db->select('first_name');
        $this->db->from('staff');
        $query = $this->db->get();
        $result = $query->result();

        $staff_id = array('-SELECT-');
        $first_name = array('-SELECT-');


        for ($i = 0; $i < count($result); $i++)
        {
            array_push($staff_id, $result[$i]->id);
            array_push($first_name, $result[$i]->first_name);

        }
        return $staff_result = array_combine($staff_id, $first_name);
    }


//    save form details
public function save($title, $url, $user_id ,$staff_id){

    $this->db->set('title',$title);
    $this->db->set('image',$url);
    $this->db->set('user_id',$user_id);
    $this->db->set('staff_id',$staff_id);
    $this->db->insert('floor_plan');

}



}