sac0o01 Posted May 21, 2011 Share Posted May 21, 2011 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 More sharing options...
Zurev Posted May 21, 2011 Share Posted May 21, 2011 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. Link to comment https://forums.phpfreaks.com/topic/237022-help-with-updating-date-in-database/#findComment-1218303 Share on other sites More sharing options...
harristweed Posted May 21, 2011 Share Posted May 21, 2011 why so much code? why not: mysql_query("update users set lastvisit = curdate() where user_id = '{$_SESSION['userId']}' "); Link to comment https://forums.phpfreaks.com/topic/237022-help-with-updating-date-in-database/#findComment-1218348 Share on other sites More sharing options...
sac0o01 Posted May 21, 2011 Author Share Posted May 21, 2011 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 Link to comment https://forums.phpfreaks.com/topic/237022-help-with-updating-date-in-database/#findComment-1218457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.