0

I have a model function like following. When I click submit, all the data gets saved in the database, but the image field value is stored as Array,Array,Array.

How to solve this issue? I am a beginner with PHP and CodeIgniter.

public function create_post($post_image){
    $slug = url_title($this->input->post('title'));
    $image = implode(',',$post_image);

    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'body' => $this->input->post('body'),
        'category_id' => $this->input->post('category_id'),
        'user_id' => $this->session->userdata('user_id'),
        'post_image' => $image
    );

    return $this->db->insert('posts', $data);
}
8
  • do you want to save image name? Commented Apr 16, 2018 at 11:04
  • @Harun Anwar, comment all codes and just add print_r($post_image) and see what is showing... then copy those code and add it here. so we can find solution for you.. Commented Apr 16, 2018 at 11:07
  • Do some debugging. Find out what the value of $post_image is. Commented Apr 16, 2018 at 11:07
  • 1
    $post_image is a $_FILES array obviously. Commented Apr 16, 2018 at 11:08
  • I got following after print_r($post_image); Array ( [images] => Array ( [name] => Array ( [0] => img.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => D:\xampp\tmp\php7052.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 622030 ) ) ) Commented Apr 16, 2018 at 11:20

2 Answers 2

0

Try following code :

public function create_post($post_image){
                    $slug = url_title($this->input->post('title'));
                    $image = implode(',',$post_image);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'category_id' => $this->input->post('category_id'),
            'user_id' => $this->session->userdata('user_id'),
            'post_image' => $image['images']['name'][0]
        );
        return $this->db->insert('posts', $data);
    }

But firstly make sure you are getting values in $post_image variable.

Sign up to request clarification or add additional context in comments.

Comments

0

replace your function with following codes.

public function create_post($post_image){
    extract($this->input->post());
    $slug = url_title($title);
    $user_id = $this->session->userdata('user_id');
    $post_image = $post_image['images']['name'][0]; 

    $this->db->insert('posts', compact('title','slug','body','category_id','user_id','post_image'));
    return ($this->db->trans_status()) ? $this->db->insert_id() : false;
}

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.