dongzuo9096 2015-12-29 09:49
浏览 55

在php codeigniter中提交表单时,图像无法插入数据库

I am trying to insert an image and data to the database from a form validation method using php codeigniter . Here is my code,problem is will not insert the data and image to the database. Please help. Controller Page

public function gallery5()
{  
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('upload');
$this->load->model('Login_set');
$page_id =$this->uri->segment(3);   
$data['e']=$this->Login_set->select1(); 
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data);
}  
public function gallery5_insert()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('events','events','required');
$this->form_validation->set_rules('description','description','required');
if ($this->form_validation->run() == true)
{       
$config['upload_path']          = 'application/views/App_stay/photo1/';
$config['max_size']             = '100';
$config['max_width']            = '1024';
$config['max_height']           = '768';
$this->load->library('upload', $config);
$field_name = "images";
if ( ! $this->upload->do_upload($field_name))
{
$error = array('error' => $this->upload->display_errors());
$page_id =$this->uri->segment(3);
$data['e']=$this->Login_set->select1_edit();       
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data, $error);
}
else 
{
$image_path = $this->upload->data();
$this->Login_set->add_events();
$page_id =$this->uri->segment(3);
$data['e']=$this->Login_set->select1_edit();      
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data);
}
} 
} 

View Page

<form action="<?php echo base_url();?>index.php/Welcome/gallery5_insert" method="post" enctype="multipart/form-data">
<?php
foreach ($e->result_array() as $value){       
?>     
<div class="form-group has-info ">
<input type="hidden" name="id" value="<?php echo $value['event_id'];?>">
<?php }?>              
<div class="form-group has-info col-lg-7">
<label class="control-label" for="inputSuccess">Event title</label>
<input type="text" class="form-control" name="events" id="events" >
</div>                                   
<div class="form-group has-info col-lg-7 ">
<label>Event Description</label>
<textarea id="description" name="description" class="form-control " rows="3"></textarea><br><br>
</div>
<div class="form-group has-info col-lg-7">
<label>Event Related images</label>
<input type="file" name="images"><br>
</div>
<div class="form-group has-info col-lg-7">
<button type="submit" class="btn btn-primary">
<span>SUBMIT</span>
</button>
</div>
</div>     
</form> 

Model page

public function add_events()
{ 
$this->form_validation->set_message('events','events','required');
$this->form_validation->set_message('description','description','required');
$this->form_validation->set_message('images','images','required');
$this->load->database();
$this->load->helper('url');
$data = array(
'event_id'=>'',
'hotel_id'=>1,
'event_title'=>$this->input->post('events'),
'event_description'=>$this->input->post('description'),
'image_path'   =>    $image_path[full_path]                       
);
$this->db->insert('event_hotel1', $data);
}
  • 写回答

2条回答 默认 最新

  • ds34222222 2015-12-29 09:56
    关注

    You need to pass $image_path to you model file

    So change

    $this->Login_set->add_events();
    

    To

    $this->Login_set->add_events($image_path);// pass your argument
    

    And in MODEL FILE

    public function add_events($image_path){......
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改