2013-02-26 117 views
-5

这是我的控制器代码。使用Codeigniter更新数据库中的图像路径?

function edit_hotel($id) { 
      $hotel = $this->hotel_model->get_hotel($id); 

      $data['title'] = 'Edit Hotel'; 

      $this->form_validation->set_rules('hotelname', 'Hotel Name', 'required|xss_clean'); 
      $this->form_validation->set_rules('hotellocation', 'Hotel Location', 'required|xss_clean'); 
      $this->form_validation->set_rules('hotelphone', 'Hotel Phone', 'required|xss_clean'); 
      $this->form_validation->set_rules('hotelimg', 'Hotel Image', 'required|xss_clean'); 
      $this->form_validation->set_rules('hotelabout', 'Hotel About', 'required|xss_clean'); 
     if ($this->form_validation->run() === true) 
       { 

       $config['upload_path'] = './images/hotelimages'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '1000000'; 
    $config['overwrite'] = TRUE; 
    $config['remove_spaces'] = TRUE; 
    $config['encrypt_name'] = FALSE; 
    print_r($config); 
    echo "<br>after config<br>"; 
    $this->load->library('upload', $config); 
    $field_name = "hotelimg"; 
    if (! $this->upload->do_upload($field_name)) 
      { 
    $error = array('error' => $this->upload->display_errors()); 

       $this->load->view('admin/edit_hotel', $error); 
       } 
       else { 
       $image_data = $this->upload->data(); 
       print_r($image_data); 
       echo "<br>after image_data<br>"; 
       $data = array(
        'hotelname'    => $this->input->post('hotelname'), 
        'hotellocation'  => $this->input->post('hotellocation'), 
        'hotelphone'   => $this->input->post('hotelphone'), 
        'hotelimg'  => $image_data['full_path'], 
        'hotelabout'   => $this->input->post('hotelabout') 
       ); 





        $this->hotel_model->update_hotel($id, $data); 

        $this->session->set_flashdata('message', "<p>Hotel updated successfully.</p>"); 

        redirect(base_url().'index.php/admin/hotel_controller/index'); 
       } 

      } 


      $data['hotel'] = $hotel; 



      $this->load->view('admin/edit_hotel', $data); 
     } 

这是模型

要充分利用数据库中的数据进行编辑表单字段

function get_hotel($id) { 
     $this->db->select('id, hotelname, hotellocation, hotelphone,hotelimg,  
hotelabout'); 
     $this->db->where('id', $id); 
     $this->db->limit(5); 
     $query = $this->db->get('hotel_content'); 

     return $query->row_array(); 
    } 

要更新数据库

public function update_hotel($id, $data) 
    { 
     $this->db->where('id', $id); 
     $this->db->update('hotel_content', $data); 
    } 

这是浏览网页

<?php $id = $hotel['id']; ?> 
<?php echo form_open("admin/hotel_controller/edit_hotel/$id");?> 
    <table width="700" border="1" cellpadding="0" cellspacing="2" align="center"> 
     <tr> 
      <td width="234" align="right"> <div align="left">Hotel Name:: </div></td> 
      <td width="454"><input type="text" name="hotelname" id="hotelname" value="<?php 
      echo $hotel['hotelname']; ?>" class="input_field"/></td> 
     </tr> 
     <tr> 
      <td width="234" align="right"> <div align="left">Hotel Location: </div></td> 
      <td><input type="text" name="hotellocation" id="hotellocation" value="<?php 
      echo $hotel['hotellocation']; ?>" class="input_field"/><?php //echo form_input($hotellocation); ?></td> 
     </tr> 
     <tr> 
      <td align="right"><div align="left">Hotel Phone:</div></td> 
      <td><input type="text" name="hotelphone" id="hotelphone" value="<?php 
      echo $hotel['hotelphone']; ?>" class="input_field"/><?php //echo form_input($hotelphone); ?></td> 
     </tr> 
     <tr> 
      <td bgcolor="#FFFFFF" align="center"><div align="left">Hotel Image:</div></td> 
      <td><input type="file" name="hotelimg" id="hotelimg" size="33" value="<?php 
      echo $hotel['hotelimg']; ?>" /><?php //echo form_input($hotelimg); ?> 
      <input type="hidden" name="hotelimg" id="hotelimg" value="<?php 
      echo $hotel['hotelimg']; ?>" /></td> 
     </tr> 
     <tr> 
      <td align="right"><div align="left">Hotel About:</div></td> 
      <td><textarea name="hotelabout" id="hotelabout" cols="34" rows="5"> <?php 
      echo $hotel['hotelabout']; ?></textarea><?php //echo form_textarea($hotelabout); ?></td> 
     </tr> 

     <tr> 
      <td>&nbsp;</td> 
      <td><?php echo form_submit('submit', 'Submit');?> 
       <input type="button" name="btnBack" id="btnBack" value="Back" onclick="window.location.href='<?php echo base_url() ?>admin/hotel_controller/index'" />   </td> 
     </tr> 
    </table> 
<?php echo form_close(); ?> 

上面的代码没有执行任何操作来更新数据库中的图像路径。 所以任何人都可以通过纠正它来帮助我。提前感谢您

所以它显示的图像的路径是这样

print_r($image_data)

Array (
[file_name] => 
[file_type] => 
[file_path] => ./images/hotelimages/(**name of the image should be here**) 
[full_path] => ./images/hotelimages/(**name of the image should be here**) 
[raw_name] => 
[orig_name] => 
[client_name] => 
[file_ext] => 
[file_size] => 
[is_image] => 
[image_width] => 
[image_height] => 
[image_type] => 
[image_size_str] => 
) 

后IMAGE_DATA

print_r($data)

Array (
[hotelname] => aditya Hotels 
[hotellocation] => Hyderbad 
[hotelphone] => 04056894236 
[hotelimg] => ./images/hotelimages/(**name of the image should be here**) 
[hotelabout] => Aditya Sarovar Premiere is a well known lodging destination among many international and local tourists. Aditya Sarovar Premiere offers some state of the art rooms and modern facilities at affordable prices. It also has a fully equipped gym and a swimming pool. Perfect abode for all business and leisure guests, Aditya Sarovar Premiere is a great place to stay when in Hyderabad. 
) 
+0

数据未更新或部分更新? – 2013-02-26 12:06:02

+0

它jst存储像“图像/ hotelimages”的图像路径,但没有图像名称正在数据库中 – 2013-02-26 12:12:48

回答

2

而不是form_open( )在视图中,使用form_open_multipart()来允许文件上传。

相关问题