update_batch in codeigniter

喜你入骨 提交于 2020-03-21 10:36:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!