need idea

Hi

Im making a invoice system and my client needs invoice numbers in a order like 1,2,3,4 etc...

what i did was add a button to get new invoice then using ajax return last invoice number and add +1 to that number and save

problem is if two people use system at same time there is a possibility that both will get same number :(

what is the best way to solve this ?

Sorry for my bad english, hope someone will understand what i meant

Thanks
LVL 3
3xtr3m3dAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Generate the invoice number when the record is saved rather than when you open a new page to create one.
0
3xtr3m3dAuthor Commented:
is there a  easy way to insert and return the id of that inserted record [id=autoincrement]?

currently im doing in this way

SELECT ID FROM tblinvoice ORDER BY ID DESC LIMIT 1
0
Greg AlexanderLead DeveloperCommented:
SELECT MAX(ID) FROM tblinvoice is a good method
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Greg AlexanderLead DeveloperCommented:
You could also insert a blank record and grab the new id created with mysql_insert_id();

$query = "INSERT INTO tblinvoice () VALUES ()";
mysql_query($query);
$new_id = mysql_insert_id();
echo "$new_id";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OP_ZaharinCommented:
- mysql have a function call LAST_INSERT_ID(); you need to create a AUTO_INCREMENT column in your table to use this function. therefore whenever data is inserted, it will handle the sequence number during the record insertion and ensuring the number is not jumped.  let the mysql handles the sequence for you.
- here is a tutorial of how to create and using it:
http://www.tutorialspoint.com/mysql/mysql-using-sequences.htm 

- if you still want to handle the sequence in a separate table, you might want to display the invoice number only after the record have been saved (on successfully saved/generate invoice page). do not hold/display the number upfront during the data entry of the invoice page, because there is a possibility that many person will get same number.
0
leakim971PluritechnicianCommented:
0
leakim971PluritechnicianCommented:
sorry guys, I did not refreshed the page
0
3xtr3m3dAuthor Commented:
galexander07 , OP_Zaharin  thanks for the information seems like best way is use mysql_insert_id()

Thanks all for the support.
0
Greg AlexanderLead DeveloperCommented:
NP, glad to help!
0
OP_ZaharinCommented:
no prob 3xtr3med :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.