So i have been at this for an hour now and getting nowhere! I am trying to insert the current date and time into the DATETIME field in my MySQL database field but it just wont update. My php script is:
$current_date = date("Y-m-d H:i:s");
$current_user_ip = $userinstance->getUserIP();
$results = $database->query("
UPDATE users
SET
last_login_ip = '".$current_user_ip."',
last_login_date = '".$current_date."'
WHERE user_row_id = '".$user_row_id."'
");
the last_login_ip updates as expected but everything i have tried to get the DATETIME to update has failed. Can anyone see what im doing wrong here?
Thanks
Hi,
Assuming that the field ‘last_login_date’ is of the type DATETIME, the following should work:
$query = "UPDATE users SET
last_login_date = $current_date,
last_login_ip = '".$current_user_ip."'
WHERE user_row_id = '".$user_row_id."'
";
$sql = mysql_query($query)
or die(mysql_error());
This is untested, so if it doesn’t work for you, then post back here and I’ll look into it further.
HTH
r937
July 26, 2012, 1:24pm
3
i don’t do php but you can avoid the problem entirely
you don’t need to grab the current datetime in php and feed that to mysql
(for one thing, it enters mysql as a string, and mysql then has to parse it and make sure its actually a valid datetime)
just use CURRENT_TIMESTAMP, which is a built-in mysql function
$query = "UPDATE users
SET last_login_date = CURRENT_TIMESTAMP
, last_login_ip = '".$current_user_ip."'
WHERE user_row_id = '".$user_row_id."'
";
r937:
i don’t do php but you can avoid the problem entirely
you don’t need to grab the current datetime in php and feed that to mysql
(for one thing, it enters mysql as a string, and mysql then has to parse it and make sure its actually a valid datetime)
just use CURRENT_TIMESTAMP, which is a built-in mysql function
$query = "UPDATE users
SET last_login_date = CURRENT_TIMESTAMP
, last_login_ip = '".$current_user_ip."'
WHERE user_row_id = '".$user_row_id."'
";
To support r937, his recommendation will process much faster than doing it in PHP. I am currently using this and I originally was foolishly trying to do it in PHP (I should know better), but now it rocks
Steve
That’s a cool trick / technique.
You live and you learn.
Cheers Rudy.
Cups
July 26, 2012, 3:14pm
6
As you specify that you are using Mysql, you can avoid mentioning last_login_ip altogether if you define a default value for this field when you set your table up.
ON UPDATE CURRENT_TIMESTAMP
[google]mysql onupdate current_timestamp[/google]
r937
July 26, 2012, 3:50pm
7
a default value for the last login ip?
i think you meant last login date
r937:
i don’t do php but you can avoid the problem entirely
you don’t need to grab the current datetime in php and feed that to mysql
(for one thing, it enters mysql as a string, and mysql then has to parse it and make sure its actually a valid datetime)
just use CURRENT_TIMESTAMP, which is a built-in mysql function
$query = "UPDATE users
SET last_login_date = CURRENT_TIMESTAMP
, last_login_ip = '".$current_user_ip."'
WHERE user_row_id = '".$user_row_id."'
";
Thanks, that’s got it working great now!