Pages

Wednesday, December 7, 2016

Membuat Kode Transaksi Otomatis menggunakan Pemograman PHP

Selamat sore.... sudah lama tidak posting, kali ini saya akan membagikan cara membuat kode otomatis pada kode primary key yang kita gunakan seperti kode transaksi, dan kode faktur lainnya. Kode otomatis ini akan secara otomatis, saya membuat kode otomatis dengan format P0001 dst, setelah proses penyimpanan di database dilakukan.Pertama buatlah database, kemudian buat tabel di database seperti dibawah ini.

Buat file php lalu simpan di htdoc
testing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<a href="https://www.blogger.com/blogger.g?blogID=8703675925861192611" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"></a><a href="https://www.blogger.com/blogger.g?blogID=" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><?php
mysql_connect('localhost','root','');
  mysql_select_db('kode');
  ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
</head>
 
<body>
<form action="" method="post">
<table class="form" width="500" border="0" align="center">
  <tr>
    <td colspan="3" align="center"><h2><strong> Kode OTOMATIS</strong></h2></td>
  </tr>
  <tr>
    <td>Kode Penumpang</td>
    <td>:</td>
    <td><input name="kp" type="text" size="5" maxlength="5" id="kp" value="<?php
  
 $h=mysql_query("select *from kode");
 if (mysql_num_rows($h)>0){
   $k=mysql_query("SELECT CONCAT( 'P' ,LPAD(( RIGHT( MAX(kode),4)+1),4,'0')) as kod FROM kode");
   $dk=mysql_fetch_array($k);
   echo $dk[0];
 } else {
  echo "P0001";
 }
 ?>" readonly="readonly" /></td>
  </tr>
 
   <tr>
    <td>Nama Penumpang</td>
    <td>:</td>
    <td><input name="nama" type="text" size="20" maxlength="20" id="kp" /></td>
  </tr>
   <tr>
    <td></td>
    <td></td>
    <td><input name="simpan" type="submit" /></td>
  </tr>
</table>
</form>
 
<?php
if (isset($_POST['simpan'])){
  mysql_query("insert into kode values ('$_POST[kp]','$_POST[nama]')");
  echo "<meta http-equiv='refresh' content='0 url=testing.php' />";
 
}
?>
</body>
</html>
Maka hasilnya

No comments:

Post a Comment