Zr Posted July 5, 2012 Share Posted July 5, 2012 I'm incredibly new to this, so I apologize now if it's something super simple that I'm missing. I've managed to get a fairly simple login/register script working (through a tutorial), but I'm having some trouble with adding a date to a 'last_date' row upon login. I don't seem to be getting any errors, but the database isn't updating. I've tried doing it a few different ways now but nothing has worked. Upon successful login I've got this: if ($username==$dbusername&&md5($password)==$dbpassword) { $_SESSION['id']=$userid; $last_date = date("Y-m-d"); $change_date = mysql_query("UPDATE last_date='$last_date' FROM users WHERE id='$userid'"); echo "Logged in! <a href='/index.php'>Click here</a> to return to the index!"; } I feel like the mysql query is done completely wrong or in the wrong spot or something. I'm still trying to figure out just how they work. Quote Link to comment https://forums.phpfreaks.com/topic/265222-database-not-updating-last-login-date/ Share on other sites More sharing options...
jbonnett Posted July 5, 2012 Share Posted July 5, 2012 Try... UPDATE 'table_name' SET last_date='$last_date' FROM users WHERE id='$userid' you will also have to add some sort of connection to the database like... $con = mysql_connect("host","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database_name", $con); mysql_query("UPDATE 'table_name' SET last_date='$last_date' FROM users WHERE id='$userid'"); mysql_close($con); Quote Link to comment https://forums.phpfreaks.com/topic/265222-database-not-updating-last-login-date/#findComment-1359216 Share on other sites More sharing options...
Zr Posted July 5, 2012 Author Share Posted July 5, 2012 I do have a connection in a functions.php file which is included and it selects the db as well. $connect = mysql_connect("localhost","dbname","**") or die("Coudn't connect!"); mysql_select_db("dbname") or die("Couldn't find DB"); This doesn't seem to be working, either. if ($username==$dbusername&&md5($password)==$dbpassword) { $_SESSION['id']=$userid; $last_date = date("Y-m-d"); mysql_query("UPDATE 'dbname' SET last_date='$last_date' FROM users WHERE id='$userid'"); echo "Logged in! <a href='/index.php'>Click here</a> to return to the index!"; } It logs in just fine, but the last_date field in the database is still 0000-00-00 Quote Link to comment https://forums.phpfreaks.com/topic/265222-database-not-updating-last-login-date/#findComment-1359225 Share on other sites More sharing options...
DavidAM Posted July 5, 2012 Share Posted July 5, 2012 I don't think that the sql is valid. You should test the result of the mysql_query call to see if it fails and print the mysql error (at least in development): // You do NOT need the FROM clause in this simple UPDATE, and the table comes BEFORE the SET clause $sql = "UPDATE users SET last_date='$last_date' WHERE id='$userid'"; if ( ! mysql_query($sql)) { print('Query Failed: ' . $sql . "<BR>\n" . mysql_error()); } Or something along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/265222-database-not-updating-last-login-date/#findComment-1359236 Share on other sites More sharing options...
Zr Posted July 5, 2012 Author Share Posted July 5, 2012 I don't think that the sql is valid. You should test the result of the mysql_query call to see if it fails and print the mysql error (at least in development): // You do NOT need the FROM clause in this simple UPDATE, and the table comes BEFORE the SET clause $sql = "UPDATE users SET last_date='$last_date' WHERE id='$userid'"; if ( ! mysql_query($sql)) { print('Query Failed: ' . $sql . "<BR>\n" . mysql_error()); } Or something along those lines. I was fairly sure that's where I went wrong with it, but wasn't even entirely sure how to check it. But it's working perfectly now Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/265222-database-not-updating-last-login-date/#findComment-1359249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.