|
-
Jan 25th, 2010, 12:31 AM
#1
Thread Starter
Addicted Member
retreiving the latest record
Hi
i m using php and mysql..I am inserting the data into the table and straight after that i want to grab the primary key for use. how do i do that
Code:
$SQL = "INSERT INTO table (name,addressType,suburb,state,postcode,contactName,number_of_standart_stb,number_of_IQ_stb) VALUES ('".$_POST['name']."','".$_POST['addressType']."','".$_POST['Suburb']."','".$_POST['State']."','".$_POST['Postcode']."','".$_POST['contactName']."','".$_POST['standardSTB']."','".$_POST['iqstb']."')";
now after inserting this record i want to use the primary key( ID) ...how do i retreive that straight away?
-
Jan 25th, 2010, 12:36 AM
#2
Re: retreiving the latest record
You posted this in the code bank! Please read the forum link carefully before you click it next time.
Edit:
Which field are you using as a primary key? If it is name then the field would need to be as unique, if it is ID you need to first insert the id into the data (usually the first piece of data inserted into the database).
Last edited by Nightwalker83; Jan 25th, 2010 at 12:49 AM.
Reason: Adding more
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 25th, 2010, 04:06 AM
#3
Re: retreiving the latest record
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 25th, 2010, 06:09 AM
#4
Re: retreiving the latest record
You need to use the mysql_insert_id function.
"Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT)."
-
Jan 25th, 2010, 10:39 AM
#5
Re: retreiving the latest record
make sure you're not just inserting into your database before sanitising your user's input; you can use mysql_real_escape_string() to do this.
Nightwalker: when you're inserting into a database, you don't need to provide the primary key as a field if auto_increment is enabled. the value inserted would have to be null for auto_increment to work, so his query is fine. he wanted to retrieve the ID of the record he just inserted -- ILMV's solution.
-
Jan 25th, 2010, 06:43 PM
#6
Thread Starter
Addicted Member
Re: retreiving the latest record
Thanks guys....i think i have got the solutions ...mysql_insert_id() function should work.....i will try it tomorrow....
Just by curiosity....say for an example ...i added a record and just after than another user added record...will my query give me my ID or the latest user's ID?
i hope you understood my question...like what happens when the record insertion takes place in parts of nanosecond.....will it still work perfectly and give me the correct ID?
-
Jan 25th, 2010, 06:49 PM
#7
Re: retreiving the latest record
You would be preforming the query of the second user so no. However, what you might be able do is store your search for use later on.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 25th, 2010, 06:53 PM
#8
Thread Starter
Addicted Member
Re: retreiving the latest record
 Originally Posted by Nightwalker83
You would be preforming the query of the second user so no. However, what you might be do is store your search for use later on.
i only have to use that ID only once...but my confusion is that by mistake (taking the last inserted data in the database ) can the query return the ID which was inserted by another user
example
id =1 is inserted by me and at the same time id =2 was inserted by another user
when i use the mysql_insert_id function ...will it return ID =1 or ID =2 ?
-
Jan 25th, 2010, 07:04 PM
#9
Re: retreiving the latest record
Well it says on the page provided by ILMV that it returns the result for the previous query, so if you had user 1 enter data then user 2 enter data followed by the ID retrieve function the result would be the id for user 2. However, if you put the ID retrieve function after each insert you get both both user 1 and 2.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 25th, 2010, 07:35 PM
#10
Re: retreiving the latest record
... nightwalker, what you're saying is completely incorrect. it would be best that if you don't know how something works, you don't try to explain it.
mysql_insert_id() works for the last query given by the current client connection, not the last query sent to the database in general.
The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions.
-
Jan 25th, 2010, 09:16 PM
#11
Re: retreiving the latest record
 Originally Posted by Nightwalker83
Well it says on the page provided by ILMV that it returns the result for the previous query, so if you had user 1 enter data then user 2 enter data followed by the ID retrieve function the result would be the id for user 2. However, if you put the ID retrieve function after each insert you get both both user 1 and 2.
Try spending two seconds reading the documentation before giving people advice which is totally wrong and misleading.
-
Feb 16th, 2010, 05:56 AM
#12
Fanatic Member
Re: retreiving the latest record
Hi,
I had also came across last_insert_id, and what's the difference between these two? so,
Will mysql_insert_id retrieves the last inserted id as per connections???
-
Feb 16th, 2010, 06:01 AM
#13
Fanatic Member
Re: retreiving the latest record
Hi,
If mysql_insert_id works based on client connections means, what will really happen for computers connected in the local network?
-
Feb 16th, 2010, 12:11 PM
#14
Re: retreiving the latest record
client connection does not mean a physical internet connection. it means a given connection from the website to the database. every new browser window you have opened on one page will theoretically have a new connection to the database.
and, what is the difference between what two? this thread is about the function mysql_insert_id() and nothing else -- and that function is pretty well explained in this thread and also in the PHP documentation.
you're posting a lot of questions lately that are basically answered by reading through the thread you're posting in. and the threads are all a few weeks old.
-
Feb 16th, 2010, 08:21 PM
#15
Fanatic Member
Re: retreiving the latest record
I simply asked about this last_insert_id
-
Feb 16th, 2010, 08:24 PM
#16
Fanatic Member
Re: retreiving the latest record
 Originally Posted by kows
you're posting a lot of questions lately that are basically answered by reading through the thread you're posting in. and the threads are all a few weeks old.
I am thinking of starting a new thread for this last_insert_id and so i continues in this thread.
-
Feb 16th, 2010, 08:32 PM
#17
Re: retreiving the latest record
 Originally Posted by bharanidharanit
I am thinking of starting a new thread
Please do so.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|