如何将数组传递给codeigniter中的函数参数

时间:2017-05-04 13:14:47

标签: php codeigniter

我正在尝试在我的数据库中添加数据,但我被困在这里。我正在尝试传递数组,但我不知道如何做到这一点,如果有任何其他错误,请帮助我。需要你的专家建议谢谢你。

控制器文件

    public function insert(){

            $username= "Hammad";
            $company= "Devlogix";
            $this->load->model('User_model');
            $this->User_model->create_users([
            'username'=> $username,
            'company'=> $company
            ]);
            }

        }

模型文件

    public function create_users($data){
        $this->db->insert('user', $data);

        }

        }

2 个答案:

答案 0 :(得分:-1)

试试这段代码:

public function insert() {

    $this->load->model(‘User_model’);

    $data = array(
        ‘username’ => ‘Hammad’,
        ‘company’ => ‘Devlogix’
    );

    $this->User_model->create_users($data);
}

答案 1 :(得分:-1)

问题是您传递的数组不正确。

尝试更改行:

        $this->User_model->create_users([
          'username'=> $username,
          'company'=> $company
        ]);

为:

        $this->User_model->create_users( array(
          'username'=> $username,
          'company'=> $company
        ) );
相关问题