DeeDee Posted April 17, 2011 Share Posted April 17, 2011 Hi guys I have a little problem of displaying the last login time via session so far I have written this: <?php session_start(); include('connectme.php'); $_SESSION['UserID'] = $UserID; $_SESSION['Log'] = $Log; $login = 'NOW()'; $query = "UPDATE users SET Log = '$login' WHERE UserID ='$UserID'"; echo 'Previous log on was ' . $_SESSION['Log'] . '</p>'; ?> Any tips are welcome thank you (if this topic exists please redirect me to that forum!! thx) Link to comment Share on other sites More sharing options...
chris Posted April 18, 2011 Share Posted April 18, 2011 Hello, Im not quite at the section in the book on sessions and and not able to read php well yet so am probably totally wrong but wouldn't you need and include for mysqli connect? 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 18, 2011 Author Share Posted April 18, 2011 Hello, Im not quite at the section in the book on sessions and and not able to read php well yet so am probably totally wrong but wouldn't you need and include for mysqli connect? Do not worry about sessions mate that is not what I am asking. The connection is in a php file called connectme.php so that I can call from it when connecting to my database table. I hope this answers your question chris and thanks for replying. Link to comment Share on other sites More sharing options...
HartleySan Posted April 18, 2011 Share Posted April 18, 2011 Don't have the means to verify this at the moment, but try the following: Replace the following: $login = 'NOW()'; $query = "UPDATE users SET Log = '$login' WHERE UserID ='$UserID'"; With this: $query = "UPDATE users SET Log = NOW() WHERE UserID ='$UserID'"; I think the usage of the single quotation marks (which interpret everything literally (i.e., not as functions, etc.) is screwing things up. 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 18, 2011 Author Share Posted April 18, 2011 Don't have the means to verify this at the moment, but try the following: Replace the following: $login = 'NOW()'; $query = "UPDATE users SET Log = '$login' WHERE UserID ='$UserID'"; With this: $query = "UPDATE users SET Log = NOW() WHERE UserID ='$UserID'"; I think the usage of the single quotation marks (which interpret everything literally (i.e., not as functions, etc.) is screwing things up. I've tried this as you said HartleySan, it still does not update the date, FEI(For Everyones Information) I am using datetime for the log function but I do not think it matters. Thanks for the suggesstion though if you do have another on mind I will welcome it. Link to comment Share on other sites More sharing options...
Matt Posted April 18, 2011 Share Posted April 18, 2011 DeeDee, I would not suggest grabbing the last login time this way: $login = 'NOW()'; The best way to do this is with a timestamp in the database! If you grab the date/time from the user`s machine, then it will depend on the user`s location/time zone. Since many of your visitors will certainly come from different parts of the world, this will cause you to lose a precise point of reference If you use a timestamp, then all dates/times will be entered relative to one time zone (i.e. GMT or wherever your server is located)! Hope this helps! Matt 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 18, 2011 Author Share Posted April 18, 2011 DeeDee, I would not suggest grabbing the last login time this way: The best way to do this is with a timestamp in the database! If you grab the date/time from the user`s machine, then it will depend on the user`s location/time zone. Since many of your visitors will certainly come from different parts of the world, this will cause you to lose a precise point of reference If you use a timestamp, then all dates/times will be entered relative to one time zone (i.e. GMT or wherever your server is located)! Hope this helps! Matt I have replaced the datetime with timestamp and still does not update, but nonetheless I appricate the information you told me on timestamp, Thank you Matt. Link to comment Share on other sites More sharing options...
HartleySan Posted April 19, 2011 Share Posted April 19, 2011 Is your query working at all? I mean, do you get an error, or does it just not update the time? I ask because I pulled out a database that I had locally and I used the following statement in phpMyAdmin to test out my suggestion: UPDATE tutorial1 SET lastupdated = NOW() WHERE id =3 Anyway, I did that, and it updated my lastupdated column (which is of the type TIMESTAMP) perfectly fine for the row where the id is 3. I'm not sure what else to tell you without you providing us with some more information first. Sorry. 1 Link to comment Share on other sites More sharing options...
Matt Posted April 19, 2011 Share Posted April 19, 2011 DeeDee, Like HartleySan, I just tested this as well and it`s working perfectly! Sorry, I wrote my reply late last night and looking at your code again I realize that you actually were attempting a "timestamp" in the database, so my apologies for the confusion. However, I think I got confused because you put the "NOW()" function call in a variable: $login = 'NOW()'; I`m sure this is perfectly fine, but I think doing that is completely unnecessary. As HartleySan said, something like the following (which is the code I used) should work fine: $q = "UPDATE users SET last_login=now() WHERE user_id={$_SESSION['user_id']} LIMIT 1"; Make sure you have a field in the database set as follows: last_login DATETIME NOT NULL Hope this helps! 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 19, 2011 Author Share Posted April 19, 2011 DeeDee, Like HartleySan, I just tested this as well and it`s working perfectly! Sorry, I wrote my reply late last night and looking at your code again I realize that you actually were attempting a "timestamp" in the database, so my apologies for the confusion. However, I think I got confused because you put the "NOW()" function call in a variable: I`m sure this is perfectly fine, but I think doing that is completely unnecessary. As HartleySan said, something like the following (which is the code I used) should work fine: $q = "UPDATE users SET last_login=now() WHERE user_id={$_SESSION['user_id']} LIMIT 1"; Make sure you have a field in the database set as follows: last_login DATETIME NOT NULL Hope this helps! Yeah I just changed back Log to datetime last night and it worked thank you both, how can i display the date and time onscreen? Say I have written the query already do I use the variable like this: echo $row['log']; or as a session echo $_SESSION['log']; Thanks Link to comment Share on other sites More sharing options...
HartleySan Posted April 20, 2011 Share Posted April 20, 2011 Well, according to an earlier post, your field name is "Log", not "log", so be careful of that. Also, I believe Larry talks about this in the book, but you should use the DATE_FORMAT() function in MySQL to format the DATETIME data the way you want it when you return it from the database. See the following for more info and examples on how to do that: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format Other than that, you echo statements look fine for outputting the info to the screen. Again, Larry talks about this sort of thing in his book quite a bit, so I would check that out for more examples and explanations. 1 Link to comment Share on other sites More sharing options...
HartleySan Posted April 21, 2011 Share Posted April 21, 2011 As an afterthought, the field I tested my recommended statement out on was the TIMESTAMP type, and it still worked perfectly fine with NOW(). Just wanted to let you know that it should work fine with TIMESTAMP as well. 1 Link to comment Share on other sites More sharing options...
Recommended Posts