Jump to content

Database not updating last login date.


Zr

Recommended Posts

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D Thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.