0

这是我的控制器插入代码
此代码将图像插入图像路径文件夹,但路径未保存在数据库中。

function add_hotel() {
      //validate form input
      $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 ', 'callback__image_upload');
      $this->form_validation->set_rules('hotelabout', 'Hotel About', 'required|xss_clean');

      if ($this->form_validation->run() == true)
      {     

         $config['upload_path'] = './images/';
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size']    = '1000000';
         $config['overwrite'] = TRUE;
         $config['remove_spaces'] = TRUE;
         $config['encrypt_name'] = FALSE;

         $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/add_hotel', $error);
                    }
    else {

        $data = array(
            'hotelname'             => $this->input->post('hotelname'),
            'hotellocation'     => $this->input->post('hotellocation'),
            'hotelphone'            => $this->input->post('hotelphone'),
            'hotelimg'      =>    $this->upload->data('hotelimg'),
            'hotelabout'            => $this->input->post('hotelabout')
                );
            print_r($data);

            $this->db->insert('hotel_content', $data);
            $this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");

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

显示的错误是:

遇到 PHP 错误
严重性:通知
消息:数组到字符串转换
文件名:mysql/mysql_driver.php
行号:552

发生数据库错误:

错误号:1054
“字段列表”中的未知列“数组”
插入到“hotel_content”(“hotelname”、“hotellocation”、“hotelphone”、“hotelimg”、“hotelabout”)值(“hotel5”、“hyd”、 '0402365477', Array, 'welcome')
文件名:G:\wamp\www\CodeIgniter\system\database\DB_driver.php
行号:330

我需要在数据库中插入路径。谁能帮我?

4

2 回答 2

2

将其他部分替换为

else {

            $data = array(
                'hotelname'             => $this->input->post('hotelname'),
                'hotellocation'     => $this->input->post('hotellocation'),
                'hotelphone'            => $this->input->post('hotelphone'),
                'hotelimg'      =>    $this->upload->data('hotelimg'),
                'hotelabout'            => $this->input->post('hotelabout')
            );
            print_r($data);

            $this->db->insert('hotel_content', $data);
            $this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");

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

有了这个

else {
            $image_path = $this->upload->data();
            $data = array(
                'hotelname'                => $this->input->post('hotelname'),
                'hotellocation'        => $this->input->post('hotellocation'),
                'hotelphone'             => $this->input->post('hotelphone'),
                'hotelimg'      =>    $image_path[full_path],
                'hotelabout'              => $this->input->post('hotelabout')
            );
            print_r($data);

            $this->db->insert('hotel_content', $data);
            $this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");

            redirect(base_url().'index.php/admin/hotel_controller/index');
            }
于 2013-02-26T05:37:18.730 回答
0

else 部分中的“$this->upload->data('hotelimg')”行返回一个数组。您只需要从中上传的路径,可以将其提取为:

$temp = $this->upload->data('hotelimg');
$uploadedPath = $temp[full_path];
于 2013-02-26T05:41:25.453 回答