1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Auto increment a non-key field

Discussion in 'Web Design & Development' started by JiminSA, Jan 26, 2013.

Thread Status:
Not open for further replies.
  1. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,332
    First Name:
    Jim
    I would like to auto increment a non-key field (Invoice Number) within a MySql table, but PHPMyAdmin complains thus:-
    is there a workaround for this, without falling back to using the index-key as the invoice number?
     
  2. colinsp

    colinsp

    Joined:
    Sep 5, 2007
    Messages:
    2,257
    First Name:
    Colin
    Jim, not really. Auto increment won't do what you want.The way I do it is with a query to the database to find the last invoice number and then add 1 to it. The query is

    Code:
    $result = mysql_query("SELECT invoice_number FROM {$table} ORDER BY invoice_number DESC LIMIT 1");
    
    Then look at the appropriate array element (0) and increment it before writing the new record.

    Alternatively you could have a table with just the last invoice number in a row in it then just use that. Seems like overkill to do it this way to me though.
     
  3. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,332
    First Name:
    Jim
    Totally cool answer as per usual from you Colin(y)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1086903

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice