php - 错误 : Unknown column 'Array' in 'field list' (codeigniter)

标签 php mysql arrays codeigniter

我收到这个错误

Unknown column 'Array' in 'field list'

将复选框列表插入数据库时​​。当我执行 print_array 时,我得到了这个结果:

Array
(
    [0] => Array
        (
            [user_id] => 3
            [project_id] => 10
            [project_type] => 5
            [project_list] => Array
                (
                    [0] => 17
                    [1] => 18
                )

        )

)

project_list应该将值插入数据库中的新行。

我的看法:<input type="checkbox" value="<?php echo $project_list['id']; ?>" name="project_list[]">这是从数据库中填充的。

我的 Controller :$this->Project_module->createProject($_POST['project_list']);

我的模块

public function createProject($project_list){

    if($project_list != null){

        $data = array();

        foreach($project_list as $project_list){
            $data[] = array(
                'user_id'          => $this->getUserId(),
                'project_id'       => $this->getProjectId(),
                'project_type'     => $this->getProjectType(),
                'project_list'     => $this->getProjectList(),
            );
        }

        $this->db->insert_batch('tblProject', $data);

        if($this->db->affected_rows()){
            return true;
        } else {
            return false;
        }

    } 

}

编辑 我是这样编辑我的foreach的

foreach($project_list as $row){
    $data = array(
        'user_id'          => $this->getUserId(),
        'project_id'       => $this->getProjectId(),
        'project_type'     => $this->getProjectType(),
        'project_list'     => $this->getProjectList(),
    );
}

但我仍然遇到同样的错误。

最佳答案

Linundus,您需要将 foreach 中的数组别名命名为不同的名称。你有

foreach ($project_list as $project_list)

你需要有这样的东西:

foreach($project_list as $list)

关于php - 错误 : Unknown column 'Array' in 'field list' (codeigniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643956/

相关文章:

php - 让未登录的用户删除mysql条目的最安全方法

mysql - 如何从单个组中获取最后一条记录

更改字符数组的输出

c++ - O* p = 新的 O[5]; p 指向什么?

php - 正确的 PHP mcrypt 加密方法?

php - MySQL JSON 编码数据

php - 为什么我无法导航到创建数据库的页面?

javascript - 自动添加复选框值并保留其名称以存储在数据库中

arrays - Bash:一个数组可以保存另一个数组的名称吗?

php - 使用PDO在mysql中插入数据的问题