0

I have to perform the edit/update operation through the same page from which I am performing the insert operation. I get error undefined $select.

This is my method :

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;
    }
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34

2 Answers2

2

Hope this will help you :

Make the correct clause order at update query means where and set clause should come first , should be like this :

Note : make sure $catId and $datavalue are not empty pls add check for them

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;
    }
}

For more :https://www.codeigniter.com/user_guide/database/query_builder.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • but one more thing i want to add i.e i have to write query to get data from the database first then update query otherwise before getting data it made update with null values everytime – Aankit Tiwari Jul 26 '18 at 05:41
  • pls check empty `$data` if not then update after that – Pradeep Jul 26 '18 at 05:53
0

to make it short, here is what i can help to you.

    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

}
curiosity
  • 834
  • 8
  • 20