[HELP] Membuat Kode Invoice gagal terus di PHP

Discussion in 'Desain Web dan Programming' started by Surya Falhiana, 26 Aug 2015.

Thread Status:
Not open for further replies.
  1. Surya Falhiana

    Surya Falhiana Beginner 1.0

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Langsung saja..minta bantuan nya all..
    saya mau bikin nomor faktur/invoice..saya coba di localhost sukses terus..
    Hasilnya : INV0001, INV0002, INV0003 dan seterusnya..
    Nah tetapi pas saya upload di hosting malah tidak berurutan. hasilnya malah INV0001 terus..jadinya Duplikat primary key..
    Kode nya seperti ini, sudah d utak atik tetep saja kalo di upload d hosting hasilnya Duplikat terus.

    $query = "SELECT max(invoice) AS MaxINV FROM orders";
    $hasil = mysql_query($query);
    $data = @mysql_fetch_array($hasil);
    $lastinv = $data['MaxINV'];
    $noUrut = (int) substr($lastinv, 4, 4);
    $noUrut++;
    $char = "INV-";
    $newInvoice = $char . sprintf("%04s", $noUrut);
     
  2. sentabi

    sentabi Expert 1.0

    Messages:
    681
    Likes Received:
    35
    Trophy Points:
    28
  3. dpnux

    dpnux Expert 1.0

    Messages:
    455
    Likes Received:
    28
    Trophy Points:
    28
    Solusi alternatif kalau masih ingin PKnya pakai varchar/char/nvarchar

    Code:
    
    $prefix = 'INV';
    /* query untuk ambil last invoice */
    
    $noUrut = intval(str_replace($prefix, '', strtoupper($lastinv)));
    $noUrut++;
    $newInvoice = $prefix . sprintf('%04s', $noUrut);
    
    
     
  4. masiqbal

    masiqbal Expert 2.0

    Messages:
    868
    Likes Received:
    250
    Trophy Points:
    63
    Sepertinya kolom invoice adalah bertipe char atau varchar, bukan integer ya. Coba begini:
    PHP:
    $query "SELECT invoice  FROM orders ORDER BY invoice DESC LIMIT 1";
    $hasil mysql_query($query);
    $data = @mysql_fetch_array($hasil);
    $lastinv $data['invoice'];
    $noUrut = (int) substr($lastinv44);
    $noUrut++;
    $char "INV-";
    $newInvoice $char str_pad(strval($noUrut),4,"0",STR_PAD_LEFT);
     
Loading...
Thread Status:
Not open for further replies.

Share This Page

Loading...