Jump to content

Help with updating date in database


sac0o01

Recommended Posts

I am trying to update last logged in entry in the database upon succesful login. 

 

I may be way off in the logic here or I may be missing something simple.  I don't get any errors and it logs in fine.  Just does not update the lastvisit field in the database. 

 

 

//record date of most recent login
	$result = mysql_query("SELECT username FROM users WHERE user_id ='".$_SESSION['userId'] . "'"); 
	$dtCreated = date('Y-m-d');
	mysql_query("UPDATE users SET lastvisit=('$dtCreated') WHERE username = $result");

Link to comment
https://forums.phpfreaks.com/topic/237022-help-with-updating-date-in-database/
Share on other sites

mysql_query creates a resource. You're updating where username equals resource id whatever.

 

So:

//record date of most recent login
	$result = mysql_query("SELECT username FROM users WHERE user_id ='".$_SESSION['userId'] . "'"); 
                $theUsername = mysql_fetch_row($result);
                $theUsername = $theUsername[0];
	$dtCreated = date('Y-m-d');
	mysql_query("UPDATE users SET lastvisit=('$dtCreated') WHERE username = $theUsername");

 

Give that a try.

Thanks to both of you, I got it going.

 

Harristweed I thought the same thing as you and had tried that but could not get it to work.

In the end the problem was coming from the fact that when I created the column in the database I did not set it to "not null".  So it would not update the row.  I fixed the null problem and it works great now.

 

Thanks again

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.