Auto Increment Field Value

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
dsingh
User
Posts: 166

Auto Increment Field Value

Post by dsingh »

Hi friends..

i wish to use auto increment field value in my demo project.

I have a table with following 4 fields

ID | EMP_ID | EMP_NAME | EMP_RANK

I want the EMP_ID filed to be an auto increment field with a PREFIX - "EMP-" (i.e EMP-1, EMP-2, EMP-3),
is this possible using phpmaker, if yes please suggest with a hint.


sangnandar
User
Posts: 980

Post by sangnandar »

I would assumed that you want to leave auto-increment intact on db-side.

If that's the case then you can simply do on php-side,
Row_Rendered(){
$this->id->ViewValue = "EMP - ".$this->id->CurrentValue; // being id is the auto-increment field.
}


dsingh
User
Posts: 166

Post by dsingh »

Thanks for your response.

But i wish to save the record in db also by inserting value in EMP_ID.
is there any way to fetch the value while add record.


sangnandar
User
Posts: 980

Post by sangnandar »

You can,

Row_Inserted(){
$val = "EMP - " . $rsnew["id"]; // if rsnew fail try rsold.
$sql = "update tableA set EMP_ID = '".$val."' where id = ".$rsnew["id"];
Execute($sql);
}

I would suggest:

  1. Instead of using php Row_Inserted(), use db before-trigger. It's faster with db trigger than php.
  2. If your db support generated column (for example, MySQL > 5.7) you should define EMP_ID as generated column. This way you could keep the ACID of your db.

dsingh
User
Posts: 166

Post by dsingh »

Thanks a lot sangnandar for your valuable time .. tested it worked with ms-access too.


Post Reply