This topic is locked

Automatically Add UserID to Database

9/29/2011 2:47:05 PM
PHPRunner General questions
D
denspad author

Hi,
I have a table called 'CompanyNotes' in this table are 5 fields:
NoteID

CompanyID => Links to Company Table

User ID => Links To Users Table

DateCreated

NoteContent
The idea is that people add notes to a company.
What I am trying to do is to automatically add the UserID to the record rather than have the user select their own name before adding the record to the database. I have read about $_SESSION("UserID") but my UserName field in my Users table holds an abbreviated name for login purposes i.e. 'auser'. So I added another field to the users table called FullName which is the users full name.
I made the UserID field read-only and added the $_SESSION("UserID") text to the field. When I Add a record the field shows the abbreviated user name as described above. However, when I go into the list view after a record is added, the UserID field is always '0'.
What I want is for the listing view to show the 'FullName' field but I want the database field UserID to retain the numeric UserID.
When I add a record I want the FullName to be displayed in the read-only field so the user doesn't have to enter it.
Can anyone help me here?
Regards,
Den.

C
cgphp 9/29/2011

In the "Before record added" event of the CompanyNotes table enter this code:

$rs = CustomQuery("SELECT UserID FROM Users WHERE UserName='".$_SESSION("UserID")."'");

$record = db_fetch_array($rs);
$values['UserID'] = $record['UserID'];



Now, showing the dropdown menu with the users list is not necessary.
Then, if you want to show the full name of the user in the list page, set the UserID field as Custom and enter this code:

$rs = CustomQuery("SELECT FullName FROM Users WHERE UserID=".$value."");

$record = db_fetch_array($rs);
$value = $record['FullName'];


Hope the fields name are correct.