mysql - php substr function for data varchar -
i have problem, want input data invoice number, when try in phpmyadmin version 5.6.16 in computer working, im upload cpanel phpmyadmin v 5.5 not working.
result in v 5.6.16 :
- inv0001 - inv0002 - inv0003
result in cpanel/hosting. v 5.5 :
- inv0001 - inv0001 - inv0001
it's duplicate, how fix it? before.
<?php $query = "select max(invoice) invoice orders"; $hasil = mysql_query($query); $data = @mysql_fetch_array($hasil); $lastinv = $data['invoice']; $nextinv= (int) substr($lastinv, 3, 4); $nextinv++; $char = "inv"; $newinvoice = $char . sprintf("%04s", $nextinv); ?>
you can try below code avoid duplicate records :
$query = "select max(invoice) invoice orders"; $hasil = mysql_query($query); $data = @mysql_fetch_array($hasil); $lastinv = $data['invoice']; $nextinv= (int) substr($lastinv, 3, 4); $nextinv++; $char = "inv"; $i = 1; while(true) // eliminating duplicacy { $newinvoice = $char . sprintf("%04s", $nextinv); $query = "select * orders invoice = ".$newinvoice; $hasil = mysql_query($query); $data = @mysql_fetch_array($hasil); if(empty($data)) break; $i++; }
Comments
Post a Comment