stevesimo Posted March 15, 2007 Share Posted March 15, 2007 Hi, I have created a form to add a new record to my table. There is a field in the table however whose value will be generated using a given text string and the id number of the record. For example suppose the ID of the record is 1030 then the string generated value will be something like thisismystring1030 Does anyone know how I can retrieve the record ID as soon as I have created the record so that I can generate the value as mentioned above? Just out of curiosity my next step after this is to create a session variable which willl store the record ID however I have never used these before. Can anyone point me in the right direction? Many thanks Steve (Blackpool) Link to comment https://forums.phpfreaks.com/topic/42835-solved-retrieve-id-of-newly-created-record/ Share on other sites More sharing options...
monk.e.boy Posted March 15, 2007 Share Posted March 15, 2007 You may be able to: INSERT INTO table (string) VALUES ( CONCAT('moo ', CAST( last_insert_id() as CHAR ) ) ) but I'm not sure if last_insert_id() will give you the correct answer... play around with it. Another idea may be: INSERT INTO table (string) VALUES ( 'tmp' ); Update table SET string=CONCAT('moo ', CAST( last_insert_id() as CHAR ) ); NOTE this is two lines of SQL that run together. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42835-solved-retrieve-id-of-newly-created-record/#findComment-207935 Share on other sites More sharing options...
zq29 Posted March 15, 2007 Share Posted March 15, 2007 You can get the id of the last inserted record with the PHP function: mysql_insert_id() Link to comment https://forums.phpfreaks.com/topic/42835-solved-retrieve-id-of-newly-created-record/#findComment-207936 Share on other sites More sharing options...
stevesimo Posted March 15, 2007 Author Share Posted March 15, 2007 I used the function and it worked exactly as needed (see below) $recID = mysql_insert_id(); Thanks for your help with this Steve Link to comment https://forums.phpfreaks.com/topic/42835-solved-retrieve-id-of-newly-created-record/#findComment-207947 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.