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



我有一个表格示例

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

如果您使用update_batch,那么您的数组格式必须如下

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

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

你好,你可以查看我的答案Codeigniter-用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');

相关内容

  • 没有找到相关文章

最新更新