
Buatlah database dengan nama test, dengan nama tabel costumer
Buat file koneksi.php
<?php
$conn=mysql_connect('localhost','root','');
mysql_select_db('test',$conn);
?>
Buat form input atau tambah data baru.
costumer.php
<div class="formset">
<form method="post">
<table align="center" >
<tr><td colspan="2" align="center"><h2>Data Customer</h2></td></tr>
<tr>
<td width="137">Id Customer</td><td width="353"><input type="text" name="ic" size="10" maxlength="10" id="ic" /></td></tr>
<tr>
<td>Nama Customer</td><td><input type="text" name="nc" size="50" maxlength="50" id="nc" /></td></tr>
<tr>
<td>Jurusan</td><td><input type="text" name="jr" size="50" maxlength="50" id="nc" /></td></tr>
<tr><td height="38" ></td><td><input name="Simpan" type="submit" value="Simpan" />
<input type="reset" value="Reset"/></td></tr>
</table>
</form>
</div>
Buat form update data.
<div class="formset"> <form method="post"> <table align="center" > <tr><td colspan="2" align="center"><h2>Data Customer</h2></td></tr> <tr> <td width="137">Id Customer</td><td width="353"><input name="ic" type="text" id="ic" value="<?php echo $xx[0] ?>" size="10" maxlength="10" readonly /></td></tr> <tr> <td>Nama Customer</td><td><input name="nc" type="text" id="nc" value="<?php echo $xx[1] ?>" size="50" maxlength="50"/></td></tr> <tr> <td>Jurusan</td><td><input name="jr" type="text" id="nc" value="<?php echo $xx[2] ?>" size="50" maxlength="50"/></td></tr> <tr><td height="38" ></td><td><input name="update" type="submit" value="Update" /> <input type="submit" value="Reset" name="reset" /></td></tr> </table> </form> </div>
Untuk menggunakan salah satu form kita gunakan if.
<?php
include 'koneksi.php';
if (isset($_GET['nocus'])){
$nokam=$_GET['nocus'];
$sa=mysql_query("select *from customer where id_costumer='$nokam'",$conn);
$xx=mysql_fetch_array($sa);
?>
//masukkan form update data.
<?php
} else {
?>
//masukkan form input/add data baru
<?php
}
Untuk koding proses simpan dan update data pastekan koding dibawah ini.
if($_SERVER['REQUEST_METHOD'] == "POST"){
if (isset($_POST['reset'])){
echo "<meta http-equiv='refresh' content='0; url=?p=customer'>";
exit;
}
$id = $_POST['ic'];
$nama = $_POST['nc'];
$jr = $_POST['jr'];
if ($id=="" || $nama=="" || $jr=="" ){
echo "<script>alert('Data belum lengkap...')</script>";
} else {
if (isset($_POST['Simpan'])){
$queri="insert into customer values ('$id','$nama','$jr')";
mysql_query($queri,$conn);
echo "<script>alert('Data sudah disimpan...')</script>";
} else if (isset($_POST['update'])){
$queri="update customer set nama_costumer='$nama',jurusan='$jr' where id_costumer='$id'";
mysql_query($queri,$conn);
echo "<meta http-equiv='refresh' content='0; url=?p=customer'>";
echo "<script>alert('Data sudah diupdate...')</script>";
}
}
}
?>
Selanjutnya menampilkan data yang tersimpan kedalam sebuah tabel, pastekan koding berikut pada bari paling bawah.
<table width="70%" border="1" align="center" class="tabel">
<tr><th width="9%">No</th>
<th width="15%" >Id Customer</th>
<th width="36%">Nama Customer</th>
<th width="26%">Jurusan</th>
<th width="20%" colspan="2">Modify</th>
</tr>
<?php
$queri="select *from customer";
$hasil=mysql_query($queri);
$jm=mysql_num_rows($hasil);
$no=1;
while ($data=mysql_fetch_array($hasil)){
echo "<tr><td align='center'>$no</td><td align='center'>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td><a href='?p=customer&nocus=$data[0]'>edit <a href='hapus.php?nocus=$data[0]' onClick=\"return confirm('Anda yakin ingin menghapus $data[1] ?')\">hapus</a></td></tr>";
$no++;
}
?>
</table>
Koding lengkapnya adalah sebagai berikut
<?php
include 'koneksi.php';
if (isset($_GET['nocus'])){
$nokam=$_GET['nocus'];
$sa=mysql_query("select *from customer where id_costumer='$nokam'",$conn);
$xx=mysql_fetch_array($sa);
?>
<div class="formset">
<form method="post">
<table align="center" >
<tr><td colspan="2" align="center"><h2>Data Customer</h2></td></tr>
<tr>
<td width="137">Id Customer</td><td width="353"><input name="ic" type="text" id="ic" value="<?php echo $xx[0] ?>" size="10" maxlength="10" readonly /></td></tr>
<tr>
<td>Nama Customer</td><td><input name="nc" type="text" id="nc" value="<?php echo $xx[1] ?>" size="50" maxlength="50"/></td></tr>
<tr>
<td>Jurusan</td><td><input name="jr" type="text" id="nc" value="<?php echo $xx[2] ?>" size="50" maxlength="50"/></td></tr>
<tr><td height="38" ></td><td><input name="update" type="submit" value="Update" />
<input type="submit" value="Reset" name="reset" /></td></tr>
</table>
</form>
</div>
<?php
} else {
?>
<div class="formset">
<form method="post">
<table align="center" >
<tr><td colspan="2" align="center"><h2>Data Customer</h2></td></tr>
<tr>
<td width="137">Id Customer</td><td width="353"><input type="text" name="ic" size="10" maxlength="10" id="ic" /></td></tr>
<tr>
<td>Nama Customer</td><td><input type="text" name="nc" size="50" maxlength="50" id="nc" /></td></tr>
<tr>
<td>Jurusan</td><td><input type="text" name="jr" size="50" maxlength="50" id="nc" /></td></tr>
<tr><td height="38" ></td><td><input name="Simpan" type="submit" value="Simpan" />
<input type="reset" value="Reset"/></td></tr>
</table>
</form>
</div>
<?php
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
if (isset($_POST['reset'])){
echo "<meta http-equiv='refresh' content='0; url=?p=customer'>";
exit;
}
$id = $_POST['ic'];
$nama = $_POST['nc'];
$jr = $_POST['jr'];
if ($id=="" || $nama=="" || $jr=="" ){
echo "<script>alert('Data belum lengkap...')</script>";
} else {
if (isset($_POST['Simpan'])){
$queri="insert into customer values ('$id','$nama','$jr')";
mysql_query($queri,$conn);
echo "<script>alert('Data sudah disimpan...')</script>";
} else if (isset($_POST['update'])){
$queri="update customer set nama_costumer='$nama',jurusan='$jr' where id_costumer='$id'";
mysql_query($queri,$conn);
echo "<meta http-equiv='refresh' content='0; url=?p=customer'>";
echo "<script>alert('Data sudah diupdate...')</script>";
}
}
}
?>
<table width="70%" border="1" align="center" class="tabel">
<tr><th width="9%">No</th>
<th width="15%" >Id Customer</th>
<th width="36%">Nama Customer</th>
<th width="26%">Jurusan</th>
<th width="20%" colspan="2">Modify</th>
</tr>
<?php
$queri="select *from customer";
$hasil=mysql_query($queri);
$jm=mysql_num_rows($hasil);
$no=1;
while ($data=mysql_fetch_array($hasil)){
echo "<tr><td align='center'>$no</td><td align='center'>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td><a href='?p=customer&nocus=$data[0]'>edit <a href='hapus.php?nocus=$data[0]' onClick=\"return confirm('Anda yakin ingin menghapus $data[1] ?')\">hapus</a></td></tr>";
$no++;
}
?>
</table>
Buat file hapus.php
hapus.php
<?php
if (isset($_GET['nocus'])){
mysql_query("delete from customer where id_costumer='$_GET[nocus]'");
echo '<meta http-equiv="refresh" content="0;URL=customer.php">';
}
?>
Hasilnya seperti dibawah ini.
No comments:
Post a Comment