无法更新codeigniter中的动态多个记录

时间:2018-01-30 07:47:41

标签: mysql codeigniter

我尝试使用public function SaveUpdate($data, $totalRecord) { for ($i = 0; $i < $totalRecord; $i++) { $id = implode(", ", $data['id']); $DeptId = implode(", ", $data['defaultdeptid']); $chktme = implode(", ", $data['checktime']); //echo $chktme. " and id is " .$id; //die(); $this->db->set('checktime', $data['checktime'][$i]); $this->db->where('ci.id', $data['id'][$i]); return $this->db->update('checkinout as ci'); } } 更新多条记录,同时更新记录,它只更新表格的第一条记录。 有我的模态

$totalRecord

其中$data是循环将根据记录执行的次数, <connectionStrings><add name="Entities" connectionString="" providerName="System.Data.EntityClient" /></connectionStrings></configuration> public Entities() : base(@"metadata = res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite.EF6;provider connection string=""data source=" + HostingEnvironment.MapPath("~/Content/DB/") + @"Login.db""") { var test1 = @"metadata = res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite.EF6;provider connection string=""data source=" + HostingEnvironment.MapPath("~/Content/DB/") + @"Login.db"""; } 包含id,deptid和checktime ..还有一件事,当我回显checktime和id时,它显示id和checktime所有记录..

1 个答案:

答案 0 :(得分:0)

它仅更新第一条记录,因为您使用的是return。 为了解决这个问题,请删除return

public function SaveUpdate($data, $totalRecord)
{
 $myReturn = "Update Succeeded";
 for ($i = 0; $i < $totalRecord; $i++) {

    //$id = implode(", ", $data['id']); //You dont need this
    //$DeptId = implode(", ", $data['defaultdeptid']); //You dont need this
    //$chktme = implode(", ", $data['checktime']); //You dont need this

    //echo $chktme. " and id is " .$id;
    //die();

    $this->db->set('checktime', $data['checktime'][$i]);
    $this->db->where('ci.id', $data['id'][$i]);

    $this->db->update('checkinout as ci');
    if(!($this->db->affected_rows())){
       $myReturn = "Unable to update ".$data['checktime'][$i];
       return $myReturn;
    }
 }
 return $myReturn;
}