My problem is simple: I have in my table a field called credit
.
I want to add 1 credit
to a connected user, so I update the field credit
. The update works well, but when I want to display the credit
, the addition has not been done.
Example: the user Joe
has logged with 2 credit
. I add him 1 credit
. It works in the database but not on the screen. The problem is solved if he disconnects and reconnects.
So, I'd like to know how can i update session data without disconnecting
Controller 'LoginArea'
$this->load->library('session');
$dataUser = array(
'id' => $ligneBDD->{'id_user'},
'name' => $ligneBDD->{'name'},
'credit' => $ligneBDD->{'credit'}
);
// i store id, name and credit in my session
$this->session->set_userdata('data', $dataUser);
And this is my update :
$update= $this->User_model->update(array(
'credit' => $userConnected['credit'] - 1
),
array(
'id_user' => $userConnected['id'])
);
You need to look at the session as a (temporary) "State" for a specific User.
If a login is performed (LoginArea), the current User-Data (and the credits) is fetched from your Database (DB permanent storage/state). This userData is then saved in the session.
But both, session and DB, are seperated and not connected.
So if you change the DB, this change is not reflected in the Session and vice-versa.
When you now update your DB, you need to update the session too. So here you have two possible solutions:
credit
attribute and save it again in the session (overwrite)==> new (updated) data will be present in Session-UserData