This topic is locked

Datetime stamping a record upon registration

8/23/2006 8:26:38 AM
PHPRunner General questions
A
andyjames author

Hi
I have been trying in vain to add a datetimestamp to one field in my master table.

I have three fields for recording user and administrator activity:

lingregister_date (date time of initial registration)

lingupdate_date (date time of record last updated by user)

lingadmin_update (date time of record last updated by administrator)
I have set the default value in Edit Format for all 3 fields to now().

I have implemented the following code on all edit pages for user/admin update time which works fine:

function BeforeEdit(&$values, $where)

{
// Parameters:

// $values - Array object.

// Each field on the Edit form represented as 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be edited
//********** Custom code ************

// put your custom code here

$values["lingadmin_update"]="now()";

return true;
// return true if you like to proceed with editing this record

// return false in other case
}


What I cannot do is add a date time stamp to the lingregister_date field. The default value now() doesn't do anything (presumably that only works if you add new records AFTER registering?). As each user only has one record that doesn't apply. Can you help please? Presumably I should use insert a record into another table in an After Successful Registration event??
Thanks in anticipation...

J
Jane 8/23/2006

Andy,
you can do it using AfterSuccessfulRegistration event:

function AfterSuccessfulRegistration()

{

global $conn;

$str = "update TableName set lingregister_date =now() where FieldName = ".$_SESSION["UserID"];

db_exec($str,$conn);

}



where TableName is your actual table name, FieldName is your actual field name where username is stored.

A
andyjames author 8/23/2006

Hi Jane
Thanks for getting back to me but it doesn't work. First it created an error, so I slightly modified the punctuation around $_SESSION["UserID"] as follows:

function AfterSuccessfulRegistration()

{

//********** Custom code ************

// put your custom code here

global $conn;

$str = "update ling_linguists set lingregister_date =now() where login ='".$_SESSION["UserID"]."'";

db_exec($str,$conn);
}


That didn't return an error but nothing is being recorded in the ling_linguists master table.

ling_linguists is NOT included in the project as a table with list/edit or any other pages. I have four views of this table each of which includes, amongst other fields, lingregister_date, lingupdate_date (the date fields) and ling_id (the key field). They do not include login (the username). However, under "define user group permissions", ling_linguists and the four views of it are set for non-administrators (normal users who would be registering) as follows:

Add.........|....Edit.........|....Delete.......|....List/View.....|....Export/Print

Disabled..|....Enabled...|....Disabled....|....Enabled.......|....Disabled
And for administrator level all the above are Enabled (ticked).
I hope that makes sense! I also forgot to mention that I am also trying to set the user's IP address. I can set their IP address upon Login to a session variable but am unable to do this or record it to the "ip" field.
Does the program only record the fields on the registration page upon registration. Would it be necessary to create hidden fields for ip and lingregister_date on the register page? (I'm using version 3.0)

Thanks for your feedback.

A
andyjames author 8/24/2006

Hi chaps,
Just wondering whether you may have had an opportunity to have a look at my query above on how to insert fields upon registration? I know you're working your socks off on the beta but your support is greatly appreciated. Thanks in advance. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=10623&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' />

J
Jane 8/24/2006

Andy,
As I understand you need to insert new record for registered user:

function AfterSuccessfulRegistration()

{

global $conn;

$str = "insert into ling_linguists (lingregister_date) values (now())";

db_exec($str,$conn);

}

A
andyjames author 8/24/2006

Hi Jane
Sorry but that doesn't work either.

I have tried the following code snippets in After successful registration, but neither works!

function AfterSuccessfulRegistration()

{

//********** Custom code ************

// put your custom code here

global $conn;

$str = "update ling_linguists set lingregister_date =now() where login ='".$_SESSION["UserID"]."'";

db_exec($str,$conn);

}


function AfterSuccessfulRegistration()

{

//********** Custom code ************

// put your custom code here

global $conn;

$str = "insert into ling_linguists (lingregister_date) values (now())";

db_exec($str,$conn);

}


Unless there is something obvious wrong with the above code, or it is not possible to update or insert tables after a registration event but only after a successful login event, then I have two possible ideas but would appreciate your feedback on how to implement them:

  1. attempt to set the lingregister_date after successful login (you register first, then log in for the first time) but test to see if the field lingregister_date is empty. If it is empty set it to now(), if it is already populated do nothing.... Not sure how to do the if... clause as this produces an error, but you probably get my logic...

function AfterSuccessfulLogin()

{

global $conn;

$strSQL = "select lingregister_date from ling_linguists where login='".$_SESSION["UserID"]."'";

$rs = db_query($strSQL,$conn);

$data=db_fetch_array($rs);
if ($data["lingregister_date"] = NULL) {

$str = "update ling_linguists set lingregister_date =now() where login ='".$_SESSION["UserID"]."'";

db_exec($str,$conn);

}

}


2) set lingregister_date as a hidden field on the register page and set default value to now(). However in version 3.0 it is not possible to set values on register page without editing the generated files and not sure how feasible that is?
3) I have made an obvious mistake and it is possible to use the After Successful registration event!
Thanks in advance.

A
andyjames author 8/24/2006

I managed to sort it out with an if clause as follows for anyone who might be in the same predicament:

function AfterSuccessfulLogin()

{

//********** Custom code ************

// put your custom code here
global $conn;

$strSQL = "select lingregister_date from ling_linguists where login='".$_SESSION["UserID"]."'";

$rs = db_query($strSQL,$conn);

$data=db_fetch_array($rs);
if ($data["lingregister_date"] == "") {

$str = "update ling_linguists set lingregister_date =now() where login ='".$_SESSION["UserID"]."'";

db_exec($str,$conn);

}


if clause tests to see if the value is blank, if it is then it updates the field.