Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2004, 11:35 PM
Authorized User
 
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Auto update timestamp field

I've tried numerous ways to get the timestamp in the database updated when a user accesses the website by using UPDATE. No matter where I place the code, the timestamp field remains as the initial registration date & time, which at creation was set to timestamp default NULL.

Here's the code that checks to see if the visitor has previously registered and if so, the timestamp should change to the present time.

<?php require_once('Connections/phototest.php'); ?>
<?php
// *** Validate request to login to this site. *****BEGINNING TO TRANSPOSE EARLY DEDDY SITE TO LATEST*******
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck))
{
      $GLOBALS['PrevUrl'] = $accesscheck;
      session_register('PrevUrl');
}

if (isset($_POST['username']))
{
        $loginUsername=$_POST['username'];
        $password=$_POST['pwd'];
        $MM_fldUserAuthorization = "userGroup";
        $MM_redirectLoginSuccess = "index.php";
        $MM_redirectLoginFailed = "login_failed.php";
        $MM_redirecttoReferrer = true;
        mysql_select_db($database_phototest, $phototest);
        $LoginRS__query=sprintf("SELECT username, pwd, userGroup FROM tbl_users2 WHERE username='%s' AND pwd='%s'",
            get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
        $LoginRS = mysql_query($LoginRS__query, $phototest) or die(mysql_error());
        $loginFoundUser = mysql_num_rows($LoginRS);

            if ($loginFoundUser)
            {
                $loginStrGroup = mysql_result($LoginRS,0,'userGroup');

                //declare two session variables and assign them
                $GLOBALS['MM_Username'] = $loginUsername;
                $GLOBALS['MM_UserGroup'] = $loginStrGroup;

                   //register the session variables
                session_register("MM_Username");
                session_register("MM_UserGroup");

                    if (isset($_SESSION['PrevUrl']) && true)
                    {
                        $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
                    }
                header("Location: " . $MM_redirectLoginSuccess );
            }
        else
        {
            header("Location: ". $MM_redirectLoginFailed );
        }
}

$query="UPDATE $tbl_users2 SET lastaccesstime = NULL
            WHERE username = '$username'";
?>
 
Old September 26th, 2004, 08:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let me guess that the lastaccesstime field is hardwired to default to NOW()...

If thta's the case, then the value for $username must be wrong... In fact, from my reading of it $username is never actually set, so your UPDATE statement reads:

UPDATE $tbl_users2 SET lastaccesstime = NULL
WHERE username = '';

In fact, just try

 echo $query

and see what it says...

Take it easy,
Dan
 
Old September 26th, 2004, 06:01 PM
Authorized User
 
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

  It appears that you're right. Here's the echo return:

UPDATE SET lastaccesstime = NULL WHERE username = ''

  As far as hardwired, I'm not sure. I created the database using phpMyAdmin wherein I set the column field "timestamp" at NULL and Default at NULL.

  It's strange in as much as this login code works fine when used with correct username and password that allows the visitor to access the site. It may be that the holding of username is only long enough, time-wise, to return the visitor to the index page instead of continuing on to UPDATE his/hers particular database user row? That was my thought and why I'd moved the $query line to various places, all without success. I know that if anything UPDATEs the row, then the timestamp will be newly set, thus the NULL being added to the timestamp field.

  On the other hand, perhaps the username and password are checked only for validity -- dropped and the program moves on to the index.

  The username is a unique field and the primary field is the visitor's ID (userID), which is merely an auto-increment head count. Maybe a row number is required in which is located the username? I tried changing username to userID -- nada!

  I chose to go this timestamp update route per code lifted from p 481, WROX Beginning PHP 4.

  It might be that I need a $_GET somewhere???

I'm baffled :(

Don





Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Dating Field anukagni Access 1 March 13th, 2006 09:53 AM
Creating an auto update date field tmswenson MySQL 3 January 4th, 2006 03:12 PM
Auto Populate a field ? mar0364 Classic ASP Databases 4 July 9th, 2004 11:10 AM
Update city field based on zip field nganb SQL Server ASP 0 April 22nd, 2004 10:30 PM
auto update table field nkrukovsky Access VBA 2 November 4th, 2003 05:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.