Codeigniter-为什么Update_batch仅更改了一行

时间:2018-11-01 07:29:35

标签: php sql codeigniter

我有一个表格示例

ID | answer1 | answer2   | question

1  | abc   | ghi    | 

2  | def   | klm    |

我想通过选择ID更新列问题 这是我获取ID表格表格的代码

$query = $this->db->query('SELECT * FROM example');
    $data=array();
    foreach($query->result() AS $value){
        $data[] = $value->id;

    } 
$implode = implode(',',$data);
//get from post
$question= $this->input->post('question');
foreach($question AS $q){

        $data[]=array(
        'id' => $implode,
        'question' => $q
        );

        }
        $this->db->update_batch('example',$data,'id');
//I use multiple add filed
<button class="add_field_button2">Add More Fields</button>
<input type="text" name="question[]" value="<?= $rowk->question;?>">

当添加2个问题以更新表时,仅更改了一行

我的数据数组

Array ( [0] => Array ( [id] => 1,2 [question] => question1 ? ) ) Array ( [0] => Array ( [id] => 1,2 [question] => question1 ) [1] => Array ( [id] => 107,108 [question] => question2 ) )

2 个答案:

答案 0 :(得分:0)

如果您使用update_batch,则您的数组格式必须像这样

Array(
    [0]=>Array(
            [id]=>1,
            [question] => question1 ?
        ),
    [1]=>Array(
        [id]=>2,
        [question] => question1 ?
    ),
    [2]=>Array(
        [id]=>107,
        [question] => question2
    )
)

我认为您的数组格式不正确

答案 1 :(得分:-1)

您好,您可以在这里查看我的答案Codeigniter - Update Multiple Row With Select2,如果它确实按了点赞按钮:-D,那么我认为这会对您有所帮助。谢谢 只需更改

foreach($question AS $q){

        $tempdata=array(
        'id' => $implode, // this must be a single value i think because by default update_batch third parm is work like "=" not "IN"  
        'question' => $q
        );
  array_push($data, $tempdata);
 }
        $this->db->update_batch('example',$data,'id');