问题
Here is my code that i used to update all the rows that am getting for that i used update_batch function but its not working properly
public function edit_project_involved($users)
{
foreach ($users as $v_user) {
$data=array('involved'=>1);
$v=$this->db->where('user_id',$v_user);
$query=$this->db->update_batch('tbl_users',$data,$v);
}
if($query)
{
return true;
}
}
my $users var_dump looks like this
C:\wamp64\www\spectra\application\models\Project_model.php:536:
array (size=3)
0 => string '40' (length=2)
1 => string '42' (length=2)
2 => string '37' (length=2)
am getting an error like this
One or more rows submitted for batch updating is missing the specified index.
Filename: C:/wamp64/www/spectra/system/database/DB_query_builder.php
Line Number: 2010
My table looks like this
user_id username involved
1 admin 0
36 siraj 0
37 faizal 0
38 nesru 0
40 jaseer 0
42 maltu 0
43 shahul 0
44 samsheera 0
var_dump($data) looks like this
C:\wamp64\www\spectra\application\models\Project_model.php:544:
array (size=3)
0 =>
array (size=2)
'user_id' => string '40' (length=2)
'involved' => int 1
1 =>
array (size=2)
'user_id' => string '42' (length=2)
'involved' => int 1
2 =>
array (size=2)
'user_id' => string '37' (length=2)
'involved' => int 1
回答1:
You not followed the rules mentioned in documentation https://www.codeigniter.com/user_guide/database/query_builder.html
Try this (notice update_batch
outside of loop and how the array made)
foreach ($users as $v_user) {
$data[] = array(
'user_id' => $v_user,
'involved'=> 1
);
}
$query = $this->db->update_batch('tbl_users',$data,'user_id');
来源:https://stackoverflow.com/questions/42594166/update-batch-in-codeigniter