Solved

Get id of newly created database entry

Posted on 2011-09-28
3
367 Views
Last Modified: 2012-05-12
Hi

I am wrinting a registration system for my students in php. I have a form that gets submited with all the info but I want certain things to get registered in the background based on selections in the form. I cant always trust that student numbers will be unique so I want to retrieve the unique id no created for the person by the mysql db.

Logic says submit the user to the db and then immediatly query the id to a variable once created. My problem is what if there is a duplicate name or student number already. Should I
just check for that before submitting or is there a better way to do this.

Tx
Quintus
0
Comment
Question by:QuintusSmit
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 125 total points
ID: 36715512
Yes, you need to check before you INSERT and if there is already an entry, make it an UPDATE instead.  PHP/MySQL has 'mysql_insert_id()' which will return the last id for an AUTO_INCREMENT column.  http://us.php.net/manual/en/function.mysql-insert-id.php
0
 
LVL 1

Author Closing Comment

by:QuintusSmit
ID: 36715642
Perfect! Thank you very much
0
 
LVL 1

Author Comment

by:QuintusSmit
ID: 36919825
Dave - could you pls have a look at  this question

http://www.experts-exchange.com/Database/Miscellaneous/Q_27382125.html

tx
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now