简体   繁体   中英

codeigniter update multiple rows with multiple where clause

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..

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');

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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM