Solved: Auto increment a non-key field

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

JiminSA

Jim
Thread Starter
Joined
Dec 15, 2011
Messages
3,400
I would like to auto increment a non-key field (Invoice Number) within a MySql table, but PHPMyAdmin complains thus:-
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
is there a workaround for this, without falling back to using the index-key as the invoice number?
 

colinsp

Colin
Joined
Sep 5, 2007
Messages
2,319
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top