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. 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 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 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 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 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
Archived
This topic is now archived and is closed to further replies.