用ID Codeigniter更新批处理模型

gkl3eglg  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在尝试按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之外,每一行都有不同的值,因为这是我试图更新的信息。

ruyhziif

ruyhziif1#

<?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。

相关问题