错误:未知列'数组'在'字段列表' (笨)

时间:2017-06-20 04:39:59

标签: php mysql arrays codeigniter

我收到此错误

  

未知列'数组'在'字段列表'

将复选框列表插入数据库时​​。当我 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[]">从数据库中填充。

我的控制器$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(),
    );
}

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

4 个答案:

答案 0 :(得分:4)

Linundus,你需要在foreach中将数组别名命名为不同的东西。

foreach ($project_list as $project_list)

你需要这样的东西:

foreach($project_list as $list)

答案 1 :(得分:0)

您已经传递了project_list所以您不想再次获取列表。您可以存储刚刚foreach字符串。在我身边与$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;
        }

    } 

}

答案 2 :(得分:0)

我已经解决了这个问题,现在我的编码工作正常。

错误原因

控制器中,我已经设置了这样的复选框设置器:

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

然后像这样调用createProject函数:

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

所以我的模块中的foreach出错了。

<强>解

在控制器中

  1. 删除此行

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

  2. 只需按行

    中的所有复选框值即可

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

  3. 在模块中

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

答案 3 :(得分:0)

        try
        {

            string query = "select * from family where parentsID = '" + Session["New"] + "'";
            using (OleDbCommand cmd3 = new OleDbCommand(query, con))
            {
                con.Open();
                OleDbDataReader myReader3 = null;
                myReader3 = cmd3.ExecuteReader();

                if (myReader3.HasRows)
                {
                    DataTable dt = new DataTable();
                    dt.Load(myReader3);

                    if (!DBNull.Value.Equals(dt.Rows[0]["childID"]))
                    {
                        label1.Text = dt.Rows[0]["childID"].ToString();
                        label2.Text = "$" + Convert.ToDecimal(dt.Rows[0]["childNetWorth"]).ToString("N2");
                        label3.Text = dt.Rows[0]["childName"].ToString();
                    }
                    else
                    {
                        label1.Text = "-ID-";
                        label2.Text = "-";
                        label3.Text = "-";
                    }

                    if (!DBNull.Value.Equals(dt.Rows[1]["childID"]))
                    {
                        label5.Text = dt.Rows[1]["childID"].ToString();
                        label6.Text = "$" + Convert.ToDecimal(dt.Rows[1]["childNetWorth"]).ToString("N2");
                        label7.Text = dt.Rows[1]["childName"].ToString();
                    }
                    else
                    {
                        label5.Text = "-ID-";
                        label6.Text = "-";
                        label7.Text = "-";
                    }

                    if (!DBNull.Value.Equals(dt.Rows[2]["childID"]))
                    {
                        label9.Text = dt.Rows[2]["childID"].ToString();
                        label10.Text = "$" + Convert.ToDecimal(dt.Rows[2]["childNetWorth"]).ToString("N2");
                        label11.Text = dt.Rows[2]["childName"].ToString();

                    }
                    else
                    {
                        label9.Text = "-ID-";
                        label10.Text = "-";
                        label11.Text = "-";
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Response.Write("Error: " + ex.ToString());
        }
        finally
        {
            con.Close();

        }

您的值位于嵌套键0中,因此如果您想访问project_id,则必须使用cqpViewLink.setAttribute("accountID", this._accountID); cqpViewLink.setAttribute("nodeID", cqpID); cqpViewLink.setAttribute("policyNumber", policyData.policyNumber); cqpViewLink.setAttribute("onclick", "AccountTab.NavigateToCQPNode(this.accountID,this.nodeID, policyNumber);");

这里的建议是检查密钥0中的值是否存在然后再打印