boneXXX Posted October 14, 2008 Share Posted October 14, 2008 Hi all, I need your advise for inserting data into database. To understand the concept could you just help me on this made up example please : such as there are ID(Primary), firstname, surname columns and I want to insert fistname data first then surname data second in different points. INSERT INTO Member(firstname) VALUES ('Max'); INSERT INTO Member(surname) VALUES('Power'); The data will be inserted in two different rows. So my question is how can I use the automatically generated ID number to insert this values into the same row in two different points. I don't does make sense or not, it is just a example that I made up. Could you help me on this please? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/128345-insert-with-where-condition/ Share on other sites More sharing options...
boneXXX Posted October 14, 2008 Author Share Posted October 14, 2008 Yeah I don't need to use INSERT I just need to UPDATE the row. But how can I tell which row to UPDATE the ID number without telling to the program manually? Quote Link to comment https://forums.phpfreaks.com/topic/128345-insert-with-where-condition/#findComment-664846 Share on other sites More sharing options...
boneXXX Posted October 14, 2008 Author Share Posted October 14, 2008 I think I need to SELECT the ID number after INSERTing the "firstname" and use that ID number to UPDATE the row when I want to insert the "surname". Am I right? Quote Link to comment https://forums.phpfreaks.com/topic/128345-insert-with-where-condition/#findComment-664849 Share on other sites More sharing options...
revraz Posted October 14, 2008 Share Posted October 14, 2008 Pretty much. You can get the ID as the row is created as well. How long after you create the Firstname are you looking to set the Surname? Same script or different? If you wait too long and you get more than 1 firstname that is the same, you're looking at some problems you'll have to figure out. Quote Link to comment https://forums.phpfreaks.com/topic/128345-insert-with-where-condition/#findComment-664924 Share on other sites More sharing options...
Barand Posted October 14, 2008 Share Posted October 14, 2008 Either INSERT INTO member (firstname, surname) VALUES ('Max', 'Power') or INSERT INTO Member(firstname) VALUES ('Max'); UPDATE Member SET surname='Power' WHERE id = LAST_INSERT_ID(); Note that LAST_INSERT_ID is only available until the current mysql connection is closed Quote Link to comment https://forums.phpfreaks.com/topic/128345-insert-with-where-condition/#findComment-665306 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.