Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Update table when user logs out?

New Here ,
Oct 24, 2009 Oct 24, 2009

Copy link to clipboard

Copied

I have a table called "users" with a field called logged_in that is updated to a "Y" when a user logs in.  That is working fine.  I need to update that a "N" when a user logs out, but I am having trouble.  I guess because it's not a submit button so I can't use the update wizard.  I am not that good at code, but have tried a few things and can't seem to get it twork.  Can anyone give me a simple code to insert to make this work?  OR even better a way that just logs out the user after 60 minutes and updates that field to "N".  Either way I could work with.  Thanks!

TOPICS
Server side applications

Views

2.9K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 24, 2009 Oct 24, 2009

Copy link to clipboard

Copied

Hello?!?

Kind of hard to offer a solution when your scripting language is unknown!

PHP solution is provided below. Add a last_login_date field with datetime to your DB table and user_id that updates user in query based on Session variable of logged in user.

// Update Online Now Status

mysql_select_db($database_XXXXXX, $XXXXXX);

$logged_in_user = "-1";

if (isset($_SESSION['login_id'])) {

$logged_in_user = $_SESSION['login_id'];

$query_online_now = "UPDATE users

SET logged_in = 'Y',

last_login_date = NOW()

WHERE user_id = $logged_in_user";

$online_now = mysql_query($query_online_now, $XXXXXX) or die(mysql_error());

}

$query_not_online_now = "UPDATE users

SET logged_in = 'N'

WHERE last_login_date < NOW() - INTERVAL 60 MINUTE";

$not_online_now = mysql_query($query_not_online_now, $XXXXXX) or die(mysql_error());

// Update Online Now Status

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 24, 2009 Oct 24, 2009

Copy link to clipboard

Copied

Sorry. php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 24, 2009 Oct 24, 2009

Copy link to clipboard

Copied

Lucky for you a php solution has been provided.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 24, 2009 Oct 24, 2009

Copy link to clipboard

Copied

Ok got it. I use the MM_Username instead of the login_id so no problem

there. Where exactly though do I input this code? At the top of page or

after the logged out code?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 25, 2009 Oct 25, 2009

Copy link to clipboard

Copied

Why do you need to update the database? Have you considered that many users will simply close their browsers or navigate to another site and not log out? So the user's session will timeout but the flag in the database will stay list them as logged in. Just make sure to account for these inconsistencies.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 25, 2009 Oct 25, 2009

Copy link to clipboard

Copied

Heya bregent,

If you've read my query you would have discovered that the script updates the database for the users logged in or logged out status regardless of whether the user actually logs out or not. If no activity from the user is made within 60 minutes then their status is updated to logged out when anyone visits the page the script is on which accounts for the inconsistencies you have mentioned.

Best,

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

Ok good, I had read the OP's message to mean log out after 60 minutes of inactivity.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

I couldn't get that to work using the MM_Username instead of login_id.

Any reason why that couldn't work instead?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

Most likely because you copied the code without further modifications and you do not have a database named XXXXXX

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

I modified the code and am getting this error message:

Unknown column 'rickscherer' in 'where clause'

The 'rickscherer' is my user name. Here is the code:

//Update Online Now Status

mysql_select_db($database_connRegister, $connRegister);

$logged_in_user="-1";

if(isset($_SESSION['MM_Username'])) {

$logged_in_user=$_SESSION['MM_Username'];

$query_online_now="UPDATE users SET logged_in='Online',

last_login_date=NOW() WHERE user_name=$logged_in_user";

$online_now=mysql_query($query_online_now, $connRegister) or

die(mysql_error());

}

$query_not_online_now="UPDATE users SET logged_in='Offline' WHERE

last_login_date<NOW() - INTERVAL 60 MINUTE";

$not_online_now=mysql_query($query_not_online_now, $connRegister) or

die(mysql_error());

What mistake did I make?

Thanks!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

You do not have your username inserted into the column of the database table 'users' for the field 'user_name' as the error message suggests. A rudimentary knowledge of MySQL and php is required to use the script. Your mistake is failure to learn php and MySQL before asking for a customized script for which you have no knowledge of how to edit in order for you to make it work for your specific situation.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

That isn't the case. I do understand how this code should be working, but

it isn't. The table "users" in the database with the column name of

"user_name" has rickscherer in one of the records. I tried all the other

usernames that are registered and now of them work. They all get the same

error message. It doesn't perform the log in function either so am I

putting the code in the wrong place? Where should it go?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

Rick, I apologize for dwfaqs rudeness.  Please post your code so we can examine where you might be making an error.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

Here is the code:

//Update Online Now Status

mysql_select_db($database_connRegister, $connRegister);

$logged_in_user="-1";

if(isset($_SESSION['MM_Username'])) {

$logged_in_user=$_SESSION['MM_Username'];

$query_online_now="UPDATE users SET logged_in='Online',

last_login_date=NOW() WHERE user_name=$logged_in_user";

$online_now=mysql_query($query_online_now, $connRegister) or

die(mysql_error()); } $query_not_online_now="UPDATE users SET

logged_in='Offline' WHERE last_login_date<NOW() - INTERVAL 60 MINUTE";

$not_online_now=mysql_query($query_not_online_now, $connRegister) or

die(mysql_error());

Any help you can give me would be nice!

Thanks,

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 26, 2009 Oct 26, 2009

Copy link to clipboard

Copied

jon,

My code has already been provided in this thread.

[Personal abuse removed by moderator]

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

His code is not what you posted.  he's altered it in an attempt to get the code that you posted to actually work.  are you ready to help him or just continue to belittle the guy?  I'm ready to help him... what should be the the focus of these forums.

If you can't handle me posting in the same discussion as you, then I don't know what to tell you. You shouldn't be flattered by any stretch, but maybe embarrassed. I have vested interest in seeing the Adobe Dreamweaver product excel (and so should you), and providing support and having a great community of members is part of that equation.  Be a good community member please.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

I just want someone to help me make it work. Yes, I am not expert (of

course, I wouldn't need your help if I was) so treating me like an idiot is

fine as long as you help me. I provided the altered code that looks like

should work to me so why isn't it?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

I really need to try to get this to work...does anyone know why it is giving

an error message? Here is the code again and what I am trying to do. I want

to set a field to online when a user logs in and back to offline after

either 60 minutes or when they log off.

//Update Online Now Status

mysql_select_db($database_connRegister, $connRegister);

$logged_in_user="-1";

if(isset($_SESSION['MM_Username'])) {

$logged_in_user=$_SESSION['MM_Username'];

$query_online_now="UPDATE users SET logged_in='Online',

last_login_date=NOW() WHERE user_name=$logged_in_user";

$online_now=mysql_query($query_online_now, $connRegister) or

die(mysql_error()); } $query_not_online_now="UPDATE users SET

logged_in='Offline' WHERE last_login_date<NOW() - INTERVAL 60 MINUTE";

$not_online_now=mysql_query($query_not_online_now, $connRegister) or

die(mysql_error());

PLEASE HELP!!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

Rick4209 wrote:

I really need to try to get this to work...does anyone know why it is giving

an error message?

Yes, the answer is very simple.

It's because DwFAQ's code assumes you are using the user's primary key (a number) to identify who's logged in. However, you're using $_SESSION['MM_Username'], which contains a string. When passing a string value to a SQL query, it needs to be in quotes. Since the query itself is in double quotes, you just need to wrap the variable that contains the name in single quotes like this:

$query_online_now="UPDATE users SET logged_in='Online',

last_login_date=NOW() WHERE user_name='$logged_in_user'";

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

Thanks, that did the trick. You did a great job helping me!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 27, 2009 Oct 27, 2009

Copy link to clipboard

Copied

Hi jon,

You are ready to help him and yet you are still unable to provide a solution. All I see is you saying that you're ready to help but I don't really think you can... that requires knowledge of scripting; like I said earlier. Don't get me wrong; I can handle you posting in the same forum as me. It's just that I don't think you can handle the actual discussion of development because alas - there is no solution provided in either of your posts in this thread. Only the statement that you are willing to help without actually helping. Hmmm... Perhaps there is some third party solution that you can direct the OP to since that seems to be the extent of your scripting knowledge and help offerings.

I have already helped the OP to the extent that I can. I provided a code and politely held hands a little longer to see if I could walk him through it. Again, as I previously mentioned and evident by the error message he is receiving, I think his DB is not setup properly according to my script. My script works perfectly for me and if you knew anything about php you would see that it should work for him as well. [Personal comment removed by moderator]

Rick wrote:
It doesn't perform the log in function either so am I putting the code in the wrong place?

Rick, You can put the code anywhere on a page that has a session started. Are you putting the code on your login page? It looks as if you are from your previous post which I've quoted as a reference. That is not where you would put the code. Add the code to a server side include and place it on every page except the login and logout pages. That way whenever anyone visits anypage the script will run and update the appropriate users based on the script conditions. You previously stated that you understood how the code works so I assumed you knew all of this already. Also you've closed gaps of spaces in the code, etc. Maintain the original script syntax and only modify the parameters as necessary. Don't be mistaken by jon's comments. I am not trying to treat you like an idiot [personal insult removed by moderator].

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 28, 2009 Oct 28, 2009

Copy link to clipboard

Copied

You are ready to help him and yet you are still unable to provide a solution

haven't even looked at it actually... too busy launching a new website, and building a framework if you must know.  i'm sure you're capable of addressing the issue without me... have at it.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2009 Oct 30, 2009

Copy link to clipboard

Copied

LATEST

Marking this thread as assumed answered.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines