【问题标题】:Array save in database instead of value in CodeIgniter数组保存在数据库中,而不是 CodeIgniter 中的值
【发布时间】:2018-09-26 02:24:16
【问题描述】:

我有一个如下的模型函数。当我点击提交时,所有数据都保存在数据库中,但图像字段值存储为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);
}

【问题讨论】:

  • 要保存图片名称吗?
  • @Harun Anwar,注释所有代码并添加 print_r($post_image) 并查看显示的内容...然后复制这些代码并将其添加到此处。所以我们可以为您找到解决方案..
  • 做一些调试。找出$post_image 的值是什么。
  • $post_image 是一个$_FILES 数组显然
  • 我在 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 ) [错误] => 数组 ( [0] => 0 ) [大小] => 数组 ( [0] => 622030 ) ) )

标签: php database codeigniter implode


【解决方案1】:

试试下面的代码:

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 变量中获取值。

【讨论】:

    【解决方案2】:

    用以下代码替换你的函数。

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

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2016-06-10
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多