studgate Posted April 22, 2008 Share Posted April 22, 2008 hi Guys, I am trying to update a table that I just insert inot the database. i insert the value and I want to get the primary key of that entry and update another field with that same value. Example: TABLE (ID, NAME, OTHERFIELD) INSERT into table (name) VALUES ('$NAME'); UPDATE table SET (OTHERFIELD) VALUES (ID) I hope I explain it properly. let me know if you have a question Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/ Share on other sites More sharing options...
947740 Posted April 22, 2008 Share Posted April 22, 2008 Just redefine the table layout. Make the "OTHERFIELD" SERIAL, like the ID. They should then be the same value. There is no point in having to manually update it when you can have MySQL do it for you. Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/#findComment-524233 Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 After the first insert, call mysql_insert_id() to get the last auto_inc key that was allocated to it. But why do you need two fields in the same record that will always have the same values? Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/#findComment-524240 Share on other sites More sharing options...
studgate Posted April 22, 2008 Author Share Posted April 22, 2008 I am creating a simple forum I want to be able to get the initial message id so when someone is replying to it, I can update the database. I can get the reply message working but the post is what I am having problem with. Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/#findComment-524257 Share on other sites More sharing options...
studgate Posted April 22, 2008 Author Share Posted April 22, 2008 mysql_insert_id is a little risky because what if two users are posting messages at the same time?? Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/#findComment-524269 Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 It returns the last id for your connection, each user has a different connection, Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/#findComment-524273 Share on other sites More sharing options...
studgate Posted April 22, 2008 Author Share Posted April 22, 2008 ok, I got it thanks, I got it working. Link to comment https://forums.phpfreaks.com/topic/102376-solved-primary-key-of-an-insert-table/#findComment-524277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.