使用codeigniter将图像的路径插入数据库

时间:2013-02-26 05:25:46

标签: php codeigniter

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

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
      “字段列表”中的未知列“数组”
      INSERT INTO`hotel_content`(`hotelname`,`hotellocation`,`hotelphone`,`hotelimg`,`hotelabout`)VALUES('hotel5','hyd','0402365477',Array,'welcome')
      文件名:G:\ wamp \ www \ CodeIgniter \ system \ database \ DB_driver.php
      行号:330

我需要在数据库中插入路径。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分: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');
            }

答案 1 :(得分:0)

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

$temp = $this->upload->data('hotelimg');
$uploadedPath = $temp[full_path];