这是我的控制器代码。
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> </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 :(得分:2)
在视图中使用 form_open_multipart()代替 form_open(),以允许文件上传。