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?
 
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
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Greg AlexanderLead DeveloperCommented:
SELECT MAX(ID) FROM tblinvoice is a good method
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.