Having this kind of thing depending on a user interaction is not a good idea, if the computer shuts down suddenly (power out, force shut down because it has crashed, etc) the data in the database will not be updated
I imagine, on the bench, you have a column online where it is set true or false, I suggest you change to a date type (datetime or timestamp), when the user logs in is set, in this field, the current date plus an interval of 5min, for example. In the frontend create a setInterval make an ajax request every 5min to refresh the field again. If a setInterval not possible, can be made this update in the database whenever the user performs some request for the backend, thus, the database data were outdated by, no more, 5min
But beware, depending on how many users have using the application at the same time, it can make the application very heavy
Since you didn’t specify which database you’re using, I figured it’s an SQL like Mysql