Update Login Date

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Update Login Date

Post by AliasBDI »

I have a login page that I need to send the current date/time to a field in the database. I am assuming the now() function. But I don't know what it would look like.

Any ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$sql = "UPDATE foo SET datecol = NOW() WHERE whatever='something'";
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

yes!!

Post by AliasBDI »

Thanks a bunch. Worked perfectly... Easy one.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

new issue...

Post by AliasBDI »

I got to work in phpMyAdmin but when I stick it on the page it does not work. Here is my code:

Code: Select all

<?php
/* Check User Script */
session_start();  // Start Session

include 'db.php';

// Convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) || (!$password)){
	echo "Please enter ALL of the information! <br />";
	include 'login.php';
	exit();
}

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND closed='0' AND active='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
	while($row = mysql_fetch_array($sql)){
	foreach( $row AS $key => $val ){
		$$key = stripslashes( $val );
	}
		// update last login
		$last_login = "UPDATE users SET last_login = NOW() WHERE username = '$username' AND password = '$password'";
		
		// Register some session variables!
		session_register('first_name');
		$_SESSION['first_name'] = $first_name;
		session_register('last_name');
		$_SESSION['last_name'] = $last_name;
		session_register('email_address');
		$_SESSION['email_address'] = $email_address;
		session_register('signup_date');
		$_SESSION['signup_date'] = $signup_date;
		session_register('userid');
		$_SESSION['userid'] = $userid;
		session_register('address');
		$_SESSION['address'] = $address;
		session_register('city');
		$_SESSION['city'] = $city;
		session_register('state');
		$_SESSION['state'] = $state;
		session_register('zip');
		$_SESSION['zip'] = $zip;
		session_register('phone1');
		$_SESSION['phone1'] = $phone1;
		session_register('phone2');
		$_SESSION['phone2'] = $phone2;
		session_register('birth');
		$_SESSION['birth'] = $birth;
		session_register('dl');
		$_SESSION['dl'] = $dl;
		session_register('username');
		$_SESSION['username'] = $username;
		session_register('password');
		$_SESSION['password'] = $password;
		session_register('decrypted_password');
		$_SESSION['decrypted_password'] = $decrypted_password;

		header("Location: userindex.php");
	}
} else {
	echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
	Please try again!<br />";
	include 'login.php';
}


?>
Any ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You do

Code: Select all

$last_login = "UPDATE users SET last_login = NOW() WHERE username = '$username' AND password = '$password'";
but never actually run it :o

Code: Select all

mysql_query($last_login) or die(mysql_error());
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

ahhh

Post by AliasBDI »

Stupid mistake... I'm a beginner. That totally slipped my mind. Thanks for your quick response!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It's always easier to spot mistakes in other peoples code, harder if it's your own ;)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

That is very true. Mainly because you usually have the image of the final result stuck to your mind.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Here is question...

Post by AliasBDI »

Here is a question along the same lines...

I am creating a variable for a query. I'm quering the MONTH field of a table in the database. It is a INT field so it has numbers (ie: January would be recorded as 1, February as 2, etc.).

What I need to do is grab the current month from the server ("now()" statement) and grabe the month of that variable as a number so it would query correctly.

For example (the code is wrong of course):

Code: Select all

$month = now(month_id);
//then the query
...SELECT * FROM table WHERE month = "$month"....
The "$month" must result in a number.
Ideas on that?

I guess the summary questions is... how do you make the current date a variable?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Ahhh, getting closer...

Post by AliasBDI »

This works...

Code: Select all

<?php 
$monthAll = date("Y-m-d H:i:s");
?>
Now I have to figure out how to format the "$monthALL" in to another variable that only shows the month. This give me an error:

Code: Select all

<?php
$monthNow = "echo FormatDateTime($monthAll, "m")";
?>
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

$monthNow = FormatDateTime($monthAll, "m");
Try that.
Image Image
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

AHHHH

Post by AliasBDI »

Worked!

Always something little...! Thanks man.
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

yeah right, don't do the echo !!
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

I do the following whenever I want the current time to be inserted into the database.

Code: Select all

UPDATE table_name SET colum1 = '".$variable1."', colum2 = '".$variable2."', date_modified = CURRENT_TIMESTAMP WHERE column1 = '".$variable."'";
So, as you can see I use the "CURRENT_TIMESTAMP" SQL command for inserting the current time.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

On a side note, this kind of thing,

Code: Select all

session_register('first_name');
$_SESSION['first_name'] = $first_name;
is going to cause problems, $_SESSION and session_register() should not be used together. Instead you should just have:

Code: Select all

$_SESSION['first_name'] = $first_name;
Mac
Post Reply