This topic is locked
[SOLVED]

 autoincrement handling for new record

1/11/2006 6:15:49 PM
PHPRunner General questions
A
arrowhot author

I have a table that has a key value that is autoincremented.

I'd like to show the user the new record number when the ADD screen is up, rather than having to add the record, save it, and then and re-enter the edit screen to view it.
Has anyone written the php code (and plugged it into a php runner project) to check the next record number that will be used and display / assign it to the new record and display it on the add screen? Or just display what number will be assigned to the current record being added?
thx!

Sergey Kornilov admin 1/12/2006

Hi,
generally it's not possible to get the autoincremented ID number beforehead.
You can use Add page-OnLoad event to get the max key value when entering Add page,

however this will show wrong value if two users open Add page simultaneously.
Here is the sample code for this:

global $conn;

$rs = mysql_query("select max(id) from mytable",$conn);

$data = mysql_fetch_array($rs, MYSQL_NUM);

echo "Key value to be assigned: ".($data[0]+1);


where id and mytableare your actual key field and table names.

F
fmf 8/27/2006

Hi,

generally it's not possible to get the autoincremented ID number beforehead.
You can use Add page-OnLoad event to get the max key value when entering Add page,

however this will show wrong value if two users open Add page simultaneously.
Here is the sample code for this:

global $conn;

$rs = mysql_query("select max(id) from mytable",$conn);

$data = mysql_fetch_array($rs, MYSQL_NUM);

echo "Key value to be assigned: ".($data[0]+1);


where id and mytableare your actual key field and table names.


Hi,
How about if I can check for other entered data like name AND email AND phone AND .. as a WHERE condition in the SQL after adding.
I think this might work. I was looking for the same thing. Knowing the record id to give it to the user. So, I was planning to do this in the after add event page:

capture few entered fileds like name, phone, email and so on.

Make a select id where name = name and so on

then send/display it to the user.
At least this will be more accurate than checking the max number since the user may take time to fill while some one else is almost ready to submit.