0

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 Answers 2

2

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');
            }
Sign up to request clarification or add additional context in comments.

Comments

0

the line "$this->upload->data('hotelimg')" in else part returns an array. You just need uploaded path from it which can be extracted as:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.