Cara membuat kode otomatis di PHP,
mulai dari M001,M002,M003 dll.
Skrip nya di bawah ini
<?php
@session_start();
include "inc/koneksi.php";
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-responsive.css">
<script type="text/javascript" src="boostrap/js/jquery-1.11.3.min"></script>
<title>Data Pelanggan</title>
</head>
<body>
<div class="container">
<fieldset>
<fieldset>
<legend>Data Pelanggan</legend>
<?php
$carikode = mysql_query("select max(kode_pelanggan) from tb_pelanggan") or die(mysql_error());
$datakode = mysql_fetch_array($carikode);
if($datakode) {
$nilaikode= substr($datakode[0], 1);
$kode = (int) $nilaikode;
$kode = $kode + 1;
$hasilkode = "M".str_pad($kode, 3, "0", STR_PAD_LEFT);
} else {
$hasilkode = "M001";
}
?>
<form action="" method="post">
<table class="table table-hover">
<tr>
<td>Kode Pelanggan</td>
<td>:</td>
<td><input type="text" name="kode_pelanggan" value="<?php echo $hasilkode; ?>"</td>
</tr>
<tr>
<td>Nama Toko/Dealer</td>
<td>:</td>
<td><input type="text" name="nama_toko"></td>
</tr>
<td>Nama Pemilik</td>
<td>:</td>
<td><input type="text" name="nama_pemilik"></td>
</tr>
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat"></td>
</tr>
<td>Telpon</td>
<td>:</td>
<td><input type="text" name="telpon"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" class="btn btn-primary" name="tambah" value="Tambah"><input style="margin-left: 2px;" type="reset" class="btn btn-danger" value="Reset"></td>
</tr>
</table>
</fieldset>
</form>
<?php
$kode_pelanggan =@$_POST['kode_pelanggan'];
$nama_toko =@$_POST['nama_toko'];
$nama_pemilik =@$_POST['nama_pemilik'];
$alamat =@$_POST['alamat'];
$telpon =@$_POST['telpon'];
$tambah_mobil =@$_POST['tambah'];
if ($tambah_mobil) {
if ($kode_mobil == '' || $merk == '' || $tipe == '' || $warna == '' || $harga == '') {
?>
<script type="text/javascript">
alert("inputan tidak boleh ada yang kosong");
</script>
<?php
} else {
mysql_query("insert into tb_mobil values ('$kode_mobil','$merk','$tipe','$warna', '$harga')") or die (mysql_error());
?>
<script type="text/javascript">
alert("Tambah data mobil baru berhasil");
window.location.href="?page=mobil";
</script>
<?php
}
}
?>
</fieldset>
</div>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>