4 Replies Latest reply: Feb 23, 2012 9:16 AM by bregent RSS

    invoice numbers

    tokick4

      Can anyone tell me how to make invoice number on a form to appear on the form using php.  I cannot remember how to pull the number from MySQL primary id auto increment.  I have a invoice form to take data and put into the database.  I want to display the next available number from the database.

       

      Thanks

       

      Jon

        • 1. Re: invoice numbers
          bregent CommunityMVP

          >I have a invoice form to take data and put into the database.

          >I want to display the next available number from the database.

           

          So you want to display the next available number as the invoice number on the form? That's got potentially serious problems. What if two users access the form? User A will get the same 'next available number' as user B. You're better off providing the invoice number only after the update occurs.

          • 2. Re: invoice numbers
            tokick4 Community Member

            How could I post the insert and then post back to another page all the info to print the invoice with number?

            • 3. Re: invoice numbers
              Burak Ueda Community Member

              Here is a simple workaround:

              1. Add 2 new fields in your database table(you can change the names as you like):
                • form_date DATE
                • form_status ENUM('1','0') DEFAULT 0
              2. One time only, update your current records and set the form_status to 1 to prevent them being purged. (UPDATE table_name SET form_status = 1)
              3. Insert a new empty record, get the last insert id and display it in the form. Also add that id into the from with a hidden field
              4. In receiving page, if the form entries are correct, update the table with new information and set the form_status to 1.
              5. Optionally in the receiving page, you can write a simple script that purges the incomplete forms older than XX days (e.g. DELETE FROM table WHERE form_date < 'YYYY-MM-DD' AND form_status = 0)
              • 4. Re: invoice numbers
                bregent CommunityMVP

                After inserting the row into the database, use the php function mysql_insert_id() to retrieve the invoice number to display for the user to print.