douya5194 2013-02-26 05:25
浏览 42
已采纳

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

Here is my controller insertion code
This code inserts image into image path folder but the path is not saving in database.

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');
                }

Error shown is:

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552

and

A Database Error Occurred:

Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `hotel_content` (`hotelname`, `hotellocation`, `hotelphone`, `hotelimg`, `hotelabout`) VALUES ('hotel5', 'hyd', '0402365477', Array, 'welcome')
Filename: G:\wamp\www\CodeIgniter\system\database\DB_driver.php
Line Number: 330

I need path to be inserted in database. Can anyone help me?

  • 写回答

2条回答 默认 最新

  • dongnang8192 2013-02-26 05:37
    关注

    replace else part with

    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');
                }
    

    with this

    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条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料