2015-08-21 3 views
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> 

Модель:

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 ""; 
    } 
} 
+0

Если вы не сделали этого https://ellislab.com/codeigniter/user-guide/general/ errors.html –

+0

i не удалось найти ошибку – ashik

+0

Вы нашли решение по вашей проблеме? – mdamia

ответ

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

} 



} 
+0

Какая ошибка и что у исправлено. ? –

Смежные вопросы