通过ID Codeigniter更新批处理模型



我正在尝试通过ID批量更新表

这是我的模型

public function update_batch_studentextend_by_studentID($data, $id = NULL) {
$this->db->update_batch($this->_table_name, $data, "studentID => $id");
return $id;
}

但是它显示了这个错误

A Database Error Occurred
One or more rows submitted for batch updating is missing the specified index.
Filename: models/Studentextend_m.php
Line Number: 41

控制器:

$post = $this->input->post();
for($i=0; $i < count($post['subject']); $i++) {
$studentExtendArray[] = array(
'studentID' => $studentID,
'subject' => $post['subject'][$i],
'subjectng' => $post['subjectng'][$i],
'subjectlg' => $post['subjectlg'][$i],
'subjectcre' => $post['subjectcre'][$i],


);
$this->db->update_batch('studentextend', $studentExtendArray, 'studentID');
数组输出:

Array (
[0] => Array
(
[studentID] => 97
[subject] => 
[subjectng] => 115
[subjectlg] => A+
[subjectcre] => 7.0
)
[1] => Array
(
[studentID] => 97
[subject] => 
[subjectng] => 110
[subjectlg] => B-
[subjectcre] => 7.0
)
[2] => Array
(
[studentID] => 97
[subject] => 
[subjectng] => 90
[subjectlg] => C-
[subjectcre] => 8.0
) )

我要做的是更新多行与"studentID"列。

数组输出是正确的,每行不同的值,除了StudentID,因为这是我试图更新的信息。但是在表中,它显示了3个数组/行相同的值。

<?php
$data = array(
array(
'id' => 1,
'name' => 'Scott',
'age' => 25,
'created_at' => date('Y-m-d H:i:s')
),
array(
'id' => 2,
'name' => 'Tom',
'age' => 30,
'created_at' => date('Y-m-d H:i:s')
)
);
$this->db->update_batch('students', $data, 'id'); ?>

所以你的函数不需要传入id,因为它在$data中,因此,如果一切正常,你可以从$data中获取id,因为你已经有了它们。

CodeIgniter 4.2

<?php
$data = array(
array(
'id' => 1,
'name' => 'Scott',
'age' => 25,
'created_at' => date('Y-m-d H:i:s')
),
array(
'id' => 2,
'name' => 'Tom',
'age' => 30,
'created_at' => date('Y-m-d H:i:s')
)
);
$db->table('students')->updateBatch($data, 'id'); 
?>

最新更新