Solved

Get id of newly created database entry

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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 …

860 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