我正在将多个表单中的多个数据插入数据库,但在照片列中遇到错误
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)
型号
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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7d3c2d4d3cec9c089cac6c2cbe7c0cac6cecb89c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a>
[password] => e02cb962ac59075b964b07152d234b70
[name] => testing
[nric] => 123123
[gender] => 0
[photo] => Array
(
[0] => Array
(
[photo] => 5955cac09b8f71.jpg
)
)
[is_active] => 0
)
[1] => Array
(
[parent_id] => 11
[email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96e2f3e5e2fff8f1b8fbf7f3fad6f1fbf7fffab8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>
[password] => e13cc542ac59075b9643s5152d234g59
[name] => testing sister
[nric] => 123123
[gender] => 0
[photo] => Array
(
[0] => Array
(
[photo] => 5955cac09b8f72.jpg
)
)
[is_active] => 0
)
)
最佳答案
您正尝试在 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;
}
}
关于mysql - 无法将数组插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838561/