Page 1 of 1

Check for record and if none found, make one record

Posted: Mon Jan 08, 2018 4:44 pm
by cdmak

Hello,

I have a script that I'm trying to adapt to the PHPMaker event in "Form_CustomValidate".
When the client logs in the system will check if there is one record in a specific table and if not it will create one record.

Basically this is how it works.

table: "profile" has a field called user_id and I check to see if there is one record with the same id as the users account in users.userid and if not I create one record that the user can then edit.

Here's how I did it in PHP, but not sure about PHPMaker. Thanks for any help.

$MyField = ew_ExecuteScalar("SELECT id FROM users WHERE profile.USER_ID=id");
$rowUser = mysqli_fetch_array( $MyField );

$USER_ID = $rowUser["USER_ID"];

if (!$USER_ID) {
// Do something here insert one record.

}else{
// Do nothing because one record exist.

}


Re: Check for record and if none found, make one record

Posted: Mon Jan 08, 2018 6:03 pm
by kirondedshem

$MyField = ew_ExecuteScalar("SELECT id FROM users WHERE profile.USER_ID=id");
$rowUser = mysqli_fetch_array( $MyField );

NOTE:ew_ExecuteScalar returns a scalar value ie a tsring interger, float etc, so the line below will not work.
Read about them in help menu but phpmaker has predefined database functions like ew_ExecuteScalar, ew_ExecuteRow, ew_ExecuteRows, ew_LoadRecordset, ew_ExecuteHtml which ideally can handle all database opretaions for you.
HINT:Try to also search for any of these functions on the forum to see how other have used them if you cant find it in help menu.

SO what you want should be something like.
NOTE: you can put it in any server events in phpmaker depending on where exactly you want it done.
I believe this should be run only once if possible so you can put it in server events->table specific->other->User_LoggedIn event as this is called once only when a user has logged in successfully, but you can put it anywhere you like as well

//get user user of currenly logged in user using PHPMAKER function
//this wil get the id of the user from the user table
$current_user_id = CurrentUserID();

//this will return a null if nothing found
$if_profile_exists = ew_ExecuteScalar("SELECT id FROM users WHERE profile.USER_ID='$current_user_id'");

//check if we have NOT found a profile record
if (!$if_profile_exists) {
// Do something here insert one record.
//you can run querys using ew_Execute
ew_Execute("insert into profile(USER_ID, field_1, field_2) VALUES('$current_user_id','value 1','value 2')");
}

WARNING: avoid using deafult php database functions if you can avoid it as you will always need to update them depending on what database you are using or php version, so always use the ew_funcions if available, read about these available functions in help menu. Ive also had to update many projects where I had used mysql_query and my_sqli in the past and it was not pretty.


Re: Check for record and if none found, make one record

Posted: Tue Jan 09, 2018 4:23 am
by cdmak

Thank you very much kirondedshem,

I really appreciated your post and help I was able to get it working.