Page 1 of 1

Update Login Date

Posted: Fri Oct 17, 2003 10:32 am
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?

Posted: Fri Oct 17, 2003 10:37 am
by markl999
$sql = "UPDATE foo SET datecol = NOW() WHERE whatever='something'";

yes!!

Posted: Fri Oct 17, 2003 11:06 am
by AliasBDI
Thanks a bunch. Worked perfectly... Easy one.

new issue...

Posted: Fri Oct 17, 2003 11:14 am
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?

Posted: Fri Oct 17, 2003 11:19 am
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());

ahhh

Posted: Fri Oct 17, 2003 11:25 am
by AliasBDI
Stupid mistake... I'm a beginner. That totally slipped my mind. Thanks for your quick response!

Posted: Fri Oct 17, 2003 11:27 am
by markl999
It's always easier to spot mistakes in other peoples code, harder if it's your own ;)

Posted: Sat Oct 18, 2003 6:52 am
by JAM
That is very true. Mainly because you usually have the image of the final result stuck to your mind.

Here is question...

Posted: Mon Oct 20, 2003 11:05 am
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?

Ahhh, getting closer...

Posted: Mon Oct 20, 2003 11:58 am
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")";
?>

Posted: Mon Oct 20, 2003 6:06 pm
by phice

Code: Select all

$monthNow = FormatDateTime($monthAll, "m");
Try that.

AHHHH

Posted: Mon Oct 20, 2003 8:02 pm
by AliasBDI
Worked!

Always something little...! Thanks man.

Posted: Mon Oct 20, 2003 8:03 pm
by richie256
yeah right, don't do the echo !!

Posted: Mon Oct 20, 2003 9:28 pm
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.

Posted: Tue Oct 21, 2003 2:53 am
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