Eaten Taik
Eaten Taik

Reputation: 938

Unable to insert array into database

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

Unknown column 'Array' in 'field list'

Controller

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)

Model

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

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

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

                )

            [is_active] => 0
        )

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

                )

            [is_active] => 0
        )

)

Upvotes: 2

Views: 1133

Answers (2)

Eaten Taik
Eaten Taik

Reputation: 938

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',
    );
}

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

And the output of those array :

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

    [1] => Array
        (
            [parent_id] => 11
            [email] => [email protected]
            [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.

Upvotes: 1

always-a-learner
always-a-learner

Reputation: 3794

You are trying to insert an array inside the for insertStudentData[key].

Try This:

 $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]);

if you are trying to insert multiple rows, then you can use insert_batch

I have tried your code on my side and it gets perfectly correct. it added the multiple rows to the table. This is what I have done.

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

Upvotes: 3

Related Questions