Thank you, your answer gives me a lot to think about. In answer to the rand() question, $invoice_number.rand(1111,9999) just gives me the random number.
Like you said, date("njis") gives a 6 - 8 character number, and the errors are easy to spot in my mySQL column:
7223564
7223669
7234452
11121
7234498
7238566
Here's my exact code:
if (strlen($invoice_number) < 4) {
$invoice_number = date("njis");
}
### INCREMENT invoice_number BY "1" IF the invoice_number ALREADY has been used before.
$p1 = "select exists(select invoice_number from sales_orders where invoice_number regexp '".$invoice_number."' and name is NOT NULL and name NOT regexp '".$name."' )";
if (@mysql_result(@mysql_query($p1),0,0) == 1) {
$orderarray = array();
$savedSQL = 'select invoice_number from sales_orders';
$savedQuery = mysql_query($savedSQL);
while($savedResult=mysql_fetch_array($savedQuery)) {
if(preg_match("/\d{4}/",$savedResult[0])) {
$orderarray[] = preg_replace('/\D/','',$savedResult[0]);
}
}
$invoice_number = (max($orderarray) + 1);
}
I cannot for the life of me think of what I can do to improve this code. Yes, it's newbie-stupid, and still uses deprecated mySQL, but I can't think of what else to do.
The code (above) is in red, because my face is red/embarassed for the dumb question :-)