执行编辑操作时出错

时间:2018-07-25 13:25:42

标签: php codeigniter codeigniter-3 codeigniter-query-builder

我必须在执行插入操作的同一页面上执行编辑/更新操作。我得到error undefined $select

这是我的方法:

function editCategory($catId,$datavalue)
{
    $data=array(
                'CategoryName' =>    $datavalue['CategoryName'],
                'Status'       =>    $datavalue['Status']
                );

    $this->db->update('tblcategory');
    $this->db->set($data);
    $this->db->where('id',$catId)

    $this->db->select('*');
    $this->db->from('tblcategory');
    $this->db->where('id',$catId);

    $query=$this->db->get()->row(); 

    //$result=$query->row_array(); 
    if($query)
    {
        return $query;
    }
    else
    {
        return false;
    }
}

2 个答案:

答案 0 :(得分:2)

希望这对您有帮助:

update查询中执行正确的子句顺序意味着whereset子句应排在第一位,应该是这样的:

注意:确保$catId$datavalue不为空,请对其进行检查

function editCategory($catId, $datavalue)
{
    $data = array(
        'CategoryName' => $datavalue['CategoryName'],
        'Status' => $datavalue['Status']
    );

    $this->db->set($data);
    $this->db->where('id',$catId);
    $this->db->update('tblcategory');


    $this->db->select('*');
    $this->db->from('tblcategory');
    $this->db->where('id',$catId);
    $query=$this->db->get()->row(); 
    if($query)
    {
        return $query;
    }
    else
    {
        return false;
    }
}

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html

答案 1 :(得分:0)

简而言之,这就是我能为您提供的帮助。

    function editCategory($catId, $datavalue)
    {
        $data = array(
            'CategoryName' => $datavalue['CategoryName'],
            'Status' => $datavalue['Status']
        );

        //$this->db->set($data); --> you don't need to set it because you already set the values above
        $this->db->where('id',$catId);
        $this->db->update('tblcategory',$data); //pass the data here to be inserted in your table

}