通过codeIgniter更新mysql数据

时间:2013-05-07 09:48:17

标签: php database 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在上面的函数中具有正确的值,但在检查数据库时,值没有正确更新。其中一些更新,其中一些不更新。

我的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

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

1 个答案:

答案 0 :(得分:0)

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

foreach($ cat_id as $ key =&gt; $ val)你的价值将在$ val