/    Sign up×
Community /Pin to ProfileBookmark

Preventing Duplicate Records

Hi,

I am using PHP/MySQL to build an application.

My application is a multi-user application where in more then 1 people will be entering data.

There is an employee table in my database [I]tbl_employee[/I] which has a [I]serial_no[/I] column. This column holds the serial nos of all the employees in the format of EMP00XX where XX is the row count of that table + 1. For example: [I]tbl_employee[/I] has 50 records, so if someone adds a new employee, the serial # for the 51th row will be something like EMP0051.

[B]Now Problem[/B]:
If there are 5 people doing the addition of employee simultaneously then all of those 5 people will be adding the next employee code as EMP0051.

[B]Question:[/B]
How can we prevent from inserting duplicate employee nos. under such scenario?

Plz Help ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachSep 24.2008 — That's simple

I got 2 solutions.

Solution 1:

Make the employee code UNIQUE in the database. This means the database wont be able to add the same value more than once in the same column.

Solution 2:

Before inserting simply query the database and see if the employee code has been entered if so simply don't insert it.
Copy linkTweet thisAlerts:
@NogDogSep 24.2008 — Sounds to me like you should be using an auto-increment integer field as the primary key field of that table. This will automatically take care of assigning a unique, sequential number for each insert. If you then need to display it in the "EMPnnnn" format, that can be done either in the select query or in your PHP output code via the applicable formatting functions, e.g.:
<i>
</i>SELECT CONCAT('EMP', LPAD(serial_no, 4, '0')) AS emp_no . . .

...or...
[code=php]
printf("Employee Nbr.: EMP%04d", $row['serial_no']);
[/code]
Copy linkTweet thisAlerts:
@phantom007authorSep 25.2008 — Is it possible to prefix 00 in my auto id?
Copy linkTweet thisAlerts:
@NogDogSep 25.2008 — In MySQL you can specify the display length of an integer field (the value in parentheses after the type when you define the column) and add the ZEROFILL argument which will left pad it with zeros (see http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html), but I generally prefer to directly control any such padding with with PHP's sprintf()/printf() functions.
Copy linkTweet thisAlerts:
@SyCoSep 25.2008 — do you always want to prefix with 2 zeros or when you get to id 100 do you want to drop to one zero so you simply have 4 digits?
×

Success!

Help @phantom007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...