If you don't want to/can't for some reason use built in MySQL, you can always do a loop where you check to see if empid exists in the database before you insert, and you keep looping and incrementing by 1 until you find a free number.....