Solved

Get id of newly created database entry

Posted on 2011-09-28
3
385 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 83

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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