简体   繁体   English

错误:“字段列表”中的未知列“ Array”(codeigniter)

[英]Error : Unknown column 'Array' in 'field list' (codeigniter)

I am getting this error 我收到此错误

Unknown column 'Array' in 'field list' “字段列表”中的未知列“数组”

When inserting checkbox list into database. 将复选框列表插入数据库时​​。 When I do print_array I am getting this result : 当我执行print_array时,我得到以下结果:

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

        )

)

The project_list value supposed to be inserted into a new row in the database. 应该将project_list值插入数据库的新行中。

my view : <input type="checkbox" value="<?php echo $project_list['id']; ?>" name="project_list[]"> which is populated from database. 我的看法<input type="checkbox" value="<?php echo $project_list['id']; ?>" name="project_list[]">从数据库中填充。

my controller : $this->Project_module->createProject($_POST['project_list']); 我的控制器$this->Project_module->createProject($_POST['project_list']);

my module 我的模块

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

    } 

}

EDIT I was edited my foreach like this 编辑我像这样编辑了我的foreach

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

But I am still getting the same error. 但是我仍然遇到同样的错误。

Linundus, you need to name the array alias in the foreach something different. Linundus,您需要在foreach中将数组别名命名为其他名称。 You have 你有

foreach ($project_list as $project_list)

you need to have something like this: 您需要具有以下内容:

foreach($project_list as $list)

You already pass the project_list So you don't want to get the list again. 您已经通过了project_list,所以您不想再获得该列表。 And you can store the just foreach string. 您可以存储just foreach字符串。 In my side confusion with $data and $data[] So test with both and let me know.... Please check before with Table fields are correct 在我看来, $data$data[]令人困惑, 同时进行测试,并让我知道...。 请在使用表字段之前检查是否正确

public function createProject($project_list){

    if($project_list != null){

        $data = array();

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

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

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

    } 

}

I have solved the problem and here is my coding that working fine now. 我已经解决了问题,这是我的编码现在可以正常工作了。

ERROR CAUSE 错误原因

In the controller I have already set the checkbox setter like this : 控制器中,我已经像这样设置了复选框设置器:

$this->Project_module->setProjectList($this->input->post('project_list'));

and then call the createProject function after like this : 然后像这样调用createProject函数:

$this->Project_module->createProject($_POST['project_list']);

So I am getting error with the foreach inside my module. 因此,我在模块内部的foreach出错了。

SOLUTION

In controller 在控制器中

  1. Remove this line 删除此行

    $this->Project_module->setProjectList($this->input->post('project_list'));

  2. Simply hold all checkbox value in line 只需将所有复选框的值保持在一行即可

    $this->Project_module->createProject($_POST['project_list']);

And in module 并在模块中

public function createProject($project_list){   // $_POST['project_list'] value pass in $project_list

    if($project_list != null){

        $data = array();

        foreach($project_list as $row){         // Do foreach to insert every value in different rows in database
            $data[] = array(
                'user_id'          => $this->getUserId(),
                'project_id'       => $this->getProjectId(),
                'project_type'     => $this->getProjectType(),
                'project_list'     => $row,
            );
        }

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

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

    } 

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

        )

)

your value is in a nested key 0 so if you want to access project_id you'll have to use $array[0]['project_id'] 您的值位于嵌套键0中,因此,如果要访问project_id,则必须使用$array[0]['project_id']

a suggestion here would be to you check if values inside key 0 exist then do print 建议您检查键0内的值是否存在,然后打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM