Make unique invoice numbers with mysql and use

1.5k views Asked by At

At the moment this is my function to create a random unique invoice number which is stored in a form's hidden field

function generate_invoice_number() {
    global $wpdb;
    $lastVisitor = $wpdb->get_results("SELECT visitorID FROM event_visitors_2014 ORDER BY visitorsID DESC LIMIT 1", ARRAY_A);
    $nr_last = $lastVisitor[0]['visitorID']; 
    $nr = 501 + $nr_last;
    $value = sprintf( '%04d', $nr );
    $number = 'LEDEXPO'.date('Y').'-'.uniqid().'-'.$value;      
    return $number; 
} 

I have a problem when multiple people are using the form at the same time, say 3 people are using the form they all have the same number generate. So i added uniqid(), so $value could be duplicated but $number should be unique? Is this correct or is there a better way?

How can i make test function to test this function on uniqueness?

regards

1

There are 1 answers

4
Mohammed Saqib Rajput On

Try This:

function generate_invoice_number()
{
global $wpdb;
$lastVisitor = $wpdb->get_results("SELECT visitorID FROM event_visitors_2014 ORDER BY visitorsID DESC LIMIT 1", ARRAY_A);
$nr_last = $lastVisitor[0]['visitorID'] + 1;
$number = date('Ymd') . $nr_last;
return $number; 
}