无法将阵列插入数据库 [英] Unable to insert array into database

查看:89
本文介绍了无法将阵列插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将来自多种表单的多个数据插入数据库,但在照片

I am inserting multiple data from multiple forms into database but encounter an error at column photo

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

Unknown column 'Array' in 'field list'

控制器

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()时,我可以获取要插入数据库中的确切数据.

When I do print_r() I can get the exact data to be inserted into the database.

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
        )

)

推荐答案

最后,我解决了我的问题.在我的控制器上,我像这样更改了分配数组:

Finally I solved my issue. From my controller I changed the assigning array like this :

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模型更改为:

And my foreach model I changed to this :

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
        )

)

顺便说一句,感谢所有尝试提供帮助的人!我很感激.

By the way, thank you for everyone that trying to help! I appreciate it.

这篇关于无法将阵列插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆