yandoo Posted March 5, 2008 Share Posted March 5, 2008 Hi there, i was hoping for some of your opinions on the reliability of this scenerio.... On Inserting a record on my database the user is directed to a page that queries to find the most recent record inserted and displays it....I know its unlikely but if multiple users were inserting a record at the same time would there be a chance the wrong record could be displayed ???? It is critical that the record displayed is the same that was just inserted (as other queries etc are depanding on its accuracy) Thanks Quote Link to comment Share on other sites More sharing options...
haku Posted March 5, 2008 Share Posted March 5, 2008 You can use the function mysql_insert_id to get the id of the last record inserted. You then pass that as a get variable to the page you are going to, and use that key to recover the necessary information on the next page. Note: in order for mysql_insert_id to work, you have to have a column named ID that is the Primary Key, and is set to auto-increment (which you should do for every table anyways, as it is good practice, and comes in handy at times like this!) Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 5, 2008 Author Share Posted March 5, 2008 Hi there, Yes that is what im doing at the moment with auto ID... But on the query just im just selecting records and Ordering them by ID DSC.... Is that what you mean?? Thanks Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 5, 2008 Share Posted March 5, 2008 THIS IS NOT THE CORRECT PLACE FOR THIS AS IT A QUISTION ABOUT MYSQL SORRY BUT TRUE! yes that what he ment......... i dont see any problam here................ the speed off the insert off a record is all about the database configuration and the way the database struture was setup.................. An Introduction to Database Normalization http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html also if mutipule users are inserting data to a database heavly look up a deadectaed database server there better for the job, but if it a normal web site with a little number off users inserting data 1- 200 same time dont bother... Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 5, 2008 Author Share Posted March 5, 2008 Ok thats cool, Thank you Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2008 Share Posted March 5, 2008 The only way for your code to guarantee it gets the id of the record that was just inserted by the current visitor is to do what haku posted. This must be done in the same script that executed the insert query. If you happen to be redirecting for some odd reason, you would need to pass that id to the next page (either on the end of the url or in a session variable.) Querying for the highest id number won't work when there are concurrent visitors inserting records, either in the same script or on a different page than the one that inserted the record. Quote Link to comment 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.