2

when I try this code:

$query = '';
foreach ($data as $row)
{
    $where = array(
                'community_id'  => $row->id,
                'user_id'       => $this->session->user_id,
                'status'        => 'invited_to_join',
            );
    $update = array(
                'status' => 'joined',
            );
    $query .= $this->db->set($update)->where($where)->get_compiled_update('community_members').'; ';
}
if ($query)
{   
    return $this->db->query($query);
}

it gives me the error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE community_members SET status = 'joined' WHERE community_id = '18' A' at line 4

this answer uses straightforward query with CASE but I don't know how to convert that into Codeigniter's Query Builder..

Community
  • 1
  • 1
dapidmini
  • 1,490
  • 2
  • 23
  • 46

2 Answers2

5

Try this Example;

You can direct use array $data['id'] like this and no need loop use here as my opine

        $this->db->set('status','joined');
        $this->db->where('community_id',$data['id']);
        $this->db->where('user_id',$this->session->user_id); 
        $this->db->where('status','invited_to_join');
        $this->db->update('community_members');
Ankur Radadiya
  • 245
  • 3
  • 11
  • thank you! I knew it has to be very simple.. I wonder why I can't see that the second and third where clause can be joined like that.. – dapidmini Dec 15 '16 at 10:57
  • $where = "'community_id'=".$data['id']." AND user_id=".$this->session->user_id." AND status='invited_to_join'"; $this->db->where($where); – Ankur Radadiya Dec 15 '16 at 11:04
2

You can also use where clause this way:

    $where = "'community_id'=".$data['id']." AND user_id=".$this->session-user_id." AND status='invited_to_join'";

    $this->db->set('status','joined');
    $this->db->where($where);
    $this->db->update('community_members');

Thank You!!!

Ankur Radadiya
  • 245
  • 3
  • 11