PHP / MySQL生成增量发票号

I have to generate in PHP invoices with incremental numbers starting from 1. My idea is to create a table with an autoincrement field starting from 1. When a user makes a payment, I look at the table for the last entry saved, I get its field value (the autoincrement one) and I make +1 to generate the invoice number and I save a new entry with the new invoice number to keep track of it. Is my idea correct or not? What about if 2 users looked at the table at the same time (2 queries)? In this case they will both get from it the same last saved value and will end up with the same invoice numbers generated. Is this something that can happen? Tahnks.

There is no need for inserting the new value because the autoincrement do that for you, if you need to track the correct invoice number onscreen, I suggest a temp table that keeps the invoice number assigned to that user, so when 2 or more users are working they will have diferent invoice numbers on screen, you can make a query that check the max id on both tables so it will never be duplicated on screen.

Although with the autoincrement id it shouldn't duplicate the invoice numbers.

No, don't do it this way. If you need to display an invoice number when the user first goes to create one, create the entry in the database on first display, you can then display this number on the screen, then update that invoice with the relevant updates to the invoice.

Without knowing what framework you're using, typically when you do an Insert command there is a return from it that allows you to access the auto-incremented ID.