pfkdesign Posted July 18, 2008 Share Posted July 18, 2008 hi guys, i have a problem and i don't know how to solve it ??? :'(, i would like to copy the value of id (which is auto-increment) to another field on the same table "when i inserting a record". ex: | table | -------------------- i would like to copy "id" to "c_id" when i insert record | id int A.PK | | c_id int | | label | -------------------- thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/115412-solved-copy-auto-increment-value-to-another-row-on-insert/ Share on other sites More sharing options...
craygo Posted July 18, 2008 Share Posted July 18, 2008 look into using mysql_insert_id(). Run your insert query, then use mysql_insert_id() to get the id then run an update query to set the id in the c_id field. Ray Quote Link to comment https://forums.phpfreaks.com/topic/115412-solved-copy-auto-increment-value-to-another-row-on-insert/#findComment-593307 Share on other sites More sharing options...
pfkdesign Posted July 18, 2008 Author Share Posted July 18, 2008 hi, so it will be in one inset operation or first i insert then update the form and it will update the field of c_id? ( i would like all in one operation) tnx Quote Link to comment https://forums.phpfreaks.com/topic/115412-solved-copy-auto-increment-value-to-another-row-on-insert/#findComment-593314 Share on other sites More sharing options...
craygo Posted July 18, 2008 Share Posted July 18, 2008 $sql = "INSERT INTO `tablename` (`label`) VALUES ('labelname')"; $res = mysql_query($sql) or die(mysql_error()); $id = mysql_insert_id(); $update = "UPDATE `tablename` SET `c_id` = '$id' WHERE `id` = '$id'"; $ures = mysql_query($update) or die(mysql_error()); Ray Quote Link to comment https://forums.phpfreaks.com/topic/115412-solved-copy-auto-increment-value-to-another-row-on-insert/#findComment-593329 Share on other sites More sharing options...
pfkdesign Posted July 18, 2008 Author Share Posted July 18, 2008 tnx Quote Link to comment https://forums.phpfreaks.com/topic/115412-solved-copy-auto-increment-value-to-another-row-on-insert/#findComment-593332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.