CodeIgniter Forums
update database with different id - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: update database with different id (/showthread.php?tid=88106)



update database with different id - muh-ramadhan - 07-23-2023

how to do update() deleted_at with different id, i tried it and its not working as it should

Controller Example
    public function RestoreData($id_user)
    {
        $dataUser = [
            'deleted_at' =>  NULL,
        ];
        $this->UserModel->update($id_user, $dataUser);
   
        $dataLogin = [
            'deleted_at' =>  NULL,
        ];
        $this->LoginModel->update($id_user, $dataLogin);
   
        session()->setFlashdata('restore', 'Complete');
        return redirect()->to('Restore');
    }

Database Example
tbl_user
id_user=999 (primary)
deleted_at=2023-07-23 20:31:46

tbl_login
id_login=1 (primary)
id_user=999
deleted_at=2023-07-23 20:31:46

Running Example
/RestoreData/$id_user
($id_user=999)

If I change tbl_login primary_key to id_user it works.

I tried the SQL Query Everything Runs Normal, So How Can the update() Function Update With a Different ID (Not Primary)


RE: update database with different id - ozornick - 07-24-2023

It is ok. The primary key is needed for the update. In this case, you need to make an additional request to get "select ... id_login where id_user = ?" or pass id_login directly to the method
third option is to use the builder to create a query

PHP Code:
$loginModel->set('deleted_at'null)->where('id_user'$id_user)->update() 



RE: update database with different id - muh-ramadhan - 07-24-2023

(07-24-2023, 02:56 AM)ozornick Wrote: It is ok. The primary key is needed for the update. In this case, you need to make an additional request to get "select ... id_login where id_user = ?" or pass id_login directly to the method
third option is to use the builder to create a query

PHP Code:
$loginModel->set('deleted_at'null)->where('id_user'$id_user)->update() 

this is working, thanks for the help.  Big Grin