无法将数组插入数据库

时间:2017-06-30 04:22:47

标签: mysql arrays codeigniter

我将多个表格中的多个数据插入数据库但在列照片

时遇到错误
  

'字段列表'中的未知列'数组'

控制器

if(!empty($_FILES['user_profile_file']['name'])){
    $filesCount = count($_FILES['user_profile_file']['name']);

    for ($i = 0; $i < $filesCount; $i++) {

        $studentImg = '';

        $_FILES['userFile']['name'] = $_FILES['user_profile_file']['name'][$i];
        $_FILES['userFile']['type'] = $_FILES['user_profile_file']['type'][$i];
        $_FILES['userFile']['tmp_name'] = $_FILES['user_profile_file']['tmp_name'][$i];
        $_FILES['userFile']['error'] = $_FILES['user_profile_file']['error'][$i];
        $_FILES['userFile']['size'] = $_FILES['user_profile_file']['size'][$i];

        $config['upload_path']          = FCPATH."uploadfiles/users/student-img";
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 0;

        $studentImgNew = uniqueId();
        $config['file_name'] = $studentImgNew;

        $this->load->library('upload', $config);

        if($this->upload->do_upload('userFile')){
            $fileData = $this->upload->data();

            $studentImg[$i]['photo'] = $fileData['file_name'];

            $studentData[] = array(
                'name'            => $_POST['user_name'][$i],
                'nric'            => $_POST['user_nric'][$i],
                'gender'          => $_POST['user_gender'][$i],
                'photo'           => $studentImg,
                'email'           => $_POST['user_email'][$i],
                'password'        => md5($_POST['user_password'][$i]),
                'is_active'       => '0',
            );
        }
    }
}

$this->User_account_model->create($studentData)

模型

function create($studentData){
    foreach ($studentData as $key => $studentDatas) {
        $insertStudentData[$key] = array(
            'parent_id'      => $parent_id,
            'email'          => $studentDatas['email'],
            'password'       => $studentDatas['password'],
            'name'           => $studentDatas['name'],
            'nric'           => $studentDatas['nric'],
            'gender'         => $studentDatas['gender'],
            'photo'          => $studentDatas['photo'],
            'is_active'      => $studentDatas['is_active'],
        );

        $this->db->insert('users_student', $insertStudentData[$key]);
    }

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

当我print_r()时,我可以获得要插入数据库的确切数据。

Array
(
    [0] => Array
        (
            [parent_id] => 11
            [email] => testing.mael@gmail.com
            [password] => e02cb962ac59075b964b07152d234b70
            [name] => testing
            [nric] => 123123
            [gender] => 0
            [photo] => Array
                (
                    [0] => Array
                        (
                            [photo] => 5955cac09b8f71.jpg
                        )

                )

            [is_active] => 0
        )

    [1] => Array
        (
            [parent_id] => 11
            [email] => testing.mael@gmail.com
            [password] => e13cc542ac59075b9643s5152d234g59
            [name] => testing sister
            [nric] => 123123
            [gender] => 0
            [photo] => Array
                (
                    [0] => Array
                        (
                            [photo] => 5955cac09b8f72.jpg
                        )

                )

            [is_active] => 0
        )

)

2 个答案:

答案 0 :(得分:3)

您正尝试在for insertStudentData[key]

中插入一个数组

试试这个:

 $insertStudentData[$key] = array(
        'parent_id'      => $parent_id,
        'email'          => $studentDatas['email'],
        'password'       => $studentDatas['password'],
        'name'           => $studentDatas['name'],
        'nric'           => $studentDatas['nric'],
        'gender'         => $studentDatas['gender'],
        'photo'          => $studentDatas['photo'],
        'is_active'      => $studentDatas['is_active']['0']['photo'],
    );

$this->db->insert('users_student', $insertStudentData[$key]);

如果您尝试插入多行,则可以使用insert_batch

我已经尝试过你的代码并且它完全正确。它将多行添加到表中。这就是我所做的。

function create($studentData){
$insertStudentData = ''; //Create a Variable
    foreach ($studentData => $studentDatas) {

        $insertStudentData[] = array(
            'parent_id'      => $parent_id,
            'email'          => $studentDatas['email'],
            'password'       => $studentDatas['password'],
            'name'           => $studentDatas['name'],
            'nric'           => $studentDatas['nric'],
            'gender'         => $studentDatas['gender'],
            'photo'          => $studentDatas['photo']['0']['photo'],
            'is_active'      => $studentDatas['is_active']
        );

    }

    $this->db->insert_batch('users_student', $insertStudentData[$key]);

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

答案 1 :(得分:1)

最后我解决了我的问题。从我的控制器我更改了分配数组:

if($this->upload->do_upload('userFile')){
    $fileData = $this->upload->data();

    $studentImg[$i]['photo'] = $fileData['file_name'];

    $studentData[] = array(
        'name'            => $_POST['user_name'][$i],
        'nric'            => $_POST['user_nric'][$i],
        'gender'          => $_POST['user_gender'][$i],
        'photo'           => $studentImg[$i]['photo'],   // Edited row
        'email'           => $_POST['user_email'][$i],
        'password'        => md5($_POST['user_password'][$i]),
        'is_active'       => '0',
    );
}

我的foreach模型我改为:

foreach ($studentData as $studentDatas) {
    $insertStudentData[] = array(
        'parent_id'      => $parent_id,
        'email'          => $studentDatas['email'],
        'password'       => $studentDatas['password'],
        'name'           => $studentDatas['name'],
        'nric'           => $studentDatas['nric'],
        'gender'         => $studentDatas['gender'],
        'photo'          => $studentDatas['photo'],
        'is_active'      => $studentDatas['is_active'],
    );
}
$this->db->insert_batch('users_student', $insertStudentData);

那些数组的输出:

Array
(
    [0] => Array
        (
            [parent_id] => 11
            [email] => testing.mael@gmail.com
            [password] => 202cb962ac59075b964b07152d234b70
            [name] => testing
            [nric] => 123123
            [gender] => 0
            [photo] => 5955cac09b8f71.jpg
            [is_active] => 0
        )

    [1] => Array
        (
            [parent_id] => 11
            [email] => testing.mael@gmail.com
            [password] => e13cc542ac59075b9643s5152d234g59
            [name] => testing sister
            [nric] => 123123
            [gender] => 0
            [photo] => 5955cac09b8f72.jpg
            [is_active] => 0
        )

)

顺便说一句,谢谢大家试图提供帮助!我很感激。