用于数据varchar的php substr函数

I have problem, I want to input data Invoice Number, when I try in phpmyadmin version 5.6.16 in my computer its working, but im upload to cpanel phpmyadmin v 5.5 its 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 to fix it? Thanks before.

<?php
  $query = "SELECT max(invoice) AS invoice FROM 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 the below code to avoid duplicate records :

$query = "SELECT max(invoice) AS invoice FROM 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 * FROM orders where invoice = ".$newInvoice;
    $hasil = mysql_query($query);
    $data  = @mysql_fetch_array($hasil);
    if(empty($data))
        break;
    $i++;
}

You might try @codeSun comment like this:

<?php
  $query = "SELECT invoice FROM orders order by invoice desc limit 1";
  $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);
?>