数组保存在数据库中而不是CodeIgniter中的值

时间:2018-04-16 11:03:30

标签: php database codeigniter implode

我有一个如下的模型功能。单击“提交”时,所有数据都将保存在数据库中,但图像字段值将存储为Array,Array,Array

如何解决这个问题?我是PHP和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);
}

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

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

但首先要确保你在$ post_image变量中获取值。

答案 1 :(得分:0)

使用以下代码替换您的功能。

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