通过codeIgniter更新mysql数据


mysql data update through codeIgniter

删除时,我调用了这个函数:

function delete_categories($id){
    $max_rank = $this->categories_model->get_max_rank();  //for getting max rank
    $rank = $this->categories_model->get_rank_by_id($id); //for getting the current rank
    $cat_id=array();
    for($i=$rank;$i<=$max_rank;$i++){
        $cat_id[] = $this->categories_model->get_id_by_rank($i); //contains the categories id that should be updated
    }
    foreach($cat_id as $id){//categories to be updated
        $rank = $this->categories_model->get_rank_by_id($id); 
        $data['fld_rank']= $rank-1;
        $this->categories_model->update_rank($id,$data);
    }
    //$this->categories_model->delete_category($id);
}

所有的代码都工作正常,当它达到update_rank($id, $data)的值没有更新,因为它应该。我的update_rank()函数是

function update_rank($id,$data){
    $this->db->where('fld_id',$id);
    $this->db->update('tbl_categories',$data);
}

变量$id和$data在上述函数之前有正确的值,但在检查数据库时,值没有正确更新。有些更新了,有些没有更新。

my tbl_categories是这样的:

fld_id  fld_name    fld_description     fld_rank    fld_status
1       Cat1         Cat1               4             0
2       Cat2         this is cat2       1             0
4       Cat4         this is cat4       2             0
5       Cat3         this is cat 3      3             0

我检查了火bug中的值,所有值都是正确的,但是当更新完成时,fld_rank没有正确更新,请帮助…

在你的代码$id表示数组$cat_id的键。所以使用

foreach($cat_id as $key=>$val)您的值将在$val