This topic is locked
[SOLVED]

Registration add a record to another table then update user

4/3/2024 8:04:17 PM
PHPRunner General questions
A
asawyer13 authorDevClub member

I have this code in the After Succesful Registration

$data = array();
$data["FileType"] = 1;
$data["AddedBy"] = 1;
$data["AddedByDateTime"] = now();
DB::Insert("Accounts", $data );

$_SESSION["AccountID"]= DB::LastId();
// update User with Account
$data = array();
$keyvalues = array();
$data["AccountID"] = $_SESSION["AccountID"];
$keyvalues["UserID"] = $userdata["UserID"];
DB::Update("Users", $data, $keyvalues );

What I am trying to do is insert a record into the Accounts table which seems to work.
Then I'm trying to get the lastid and store it in session variable AccountID

Then I am trying to update the user record with that AccountID.
I am thinking that the $userdata["UserID"] maybe doesn't have the UserID in it or the LastID didn't work.

What I'm getting is no AccountID in the Users record when it completes the registration.

Can someone tell me a better way or tell me what I'm doing wrong?
I am using sql server.

Thanks
Alan

A
asawyer13 authorDevClub member 4/3/2024

I have confirmed that it is getting the lastid correctly.

So it seems like the issue is in the update statement for users.

I'm going to try to confirm that the UserID is valid.

Alan

A
asawyer13 authorDevClub member 4/3/2024

It appears that the $userdata["UserID"] field is blank.

The column in the database for sure is called UserID, so I don't exactly know what I'm doing wrong.

Alan

A
asawyer13 authorDevClub member 4/3/2024

The manual says:
$userdata - an array with the user-entered data. Access fields by
$userdata["FieldName"].

It's saying user-entered data so
Does that mean that the UserID field wouldn't have a value yet?? And if so what do I do?

Sergey Kornilov admin 4/4/2024

$userdata["UserID"] will be empty if UserID is not entered by user manually as a part of the form.

Maybe you can retrive this UserID from Users table using something else that user entered, like their email address. Or you can get the maximum value of UserID column in Users table if UserID is an auto-increment.