Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get id of newly created database entry

Posted on 2011-09-28
3
Medium Priority
?
410 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 84

Accepted Solution

by:
Dave Baldwin earned 500 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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