Nomor 1
1. Buat file tampil.php dan copas/ketik coding dibawah ini :
<?php
$query = $koneksi->query("SELECT * FROM barang");
$i = 1;
?>
<h1>Tampil Data Barang</h1>
<table border=1>
<tr>
<td>No</td>
<td>Kode Barang</td>
<td>Nama Barang</td>
<td>Harga</td>
<td>Aksi</td>
</tr> <?php
while ($data = $query->fetch_array())
{
$kode = $data['KdBrg'];
echo "
<tr>
<td>" . $i++ . "</td>
<td>" . $data['KdBrg'] . "</td>
<td>" . $data['NmBrg'] . "</td>
<td>Rp. " . number_format($data['Harga'],0,",",".") . ",-</td>
<td><a href='index.php?hal=ubah.php&kode=$kode'>Ubah</a> | <a href='index.php?hal=hapus.php&kode=$kode'>Hapus</a> </td>
</tr>";
} ?>
</table>
2. Buat file php dengan nama "ubah.php" dan ketik/copas coding dibawah ini :
<?php
$kode = $_GET["kode"];
$query = $koneksi->query("SELECT * FROM barang WHERE KdBrg LIKE '$kode'");
$data = $query->fetch_array()
?>
<form method="POST">
<h1>Ubah Data Barang</h1>
<table>
<tr>
<td>Kode Barang</td>
<td>:</td>
<td><input type="text" name="txtKd" disabled='disabled' value="<?php echo $data['KdBrg']; ?>"></td>
</tr>
<tr>
<td>Nama Barang</td>
<td>:</td>
<td><input type="text" name="txtNm" value="<?php echo $data['NmBrg']; ?>"></td>
</tr>
<tr>
<td>Harga</td>
<td>:</td>
<td><input type="text" name="txtHr" onkeyup="this.value=this.value.replace(/[^\d]/,'')" value="<?php echo $data['Harga']; ?>"></td>
</tr>
<tr>
<td colspan="3">
<input type="hidden" name="txtKd" value="<?php echo $data['KdBrg']; ?>">
<input type="submit" value="Ubah" name="btnubah">
</td>
</tr>
</table>
<form/>
<?php
if (isset($_POST["btnubah"]))
{
#Tampung ke variabel yang isian dari form
$kode = $_POST["txtKd"];
$nama = $_POST["txtNm"];
$harga = $_POST["txtHr"];
#echo $kode . $nama . $harga;
$query = $koneksi->query("UPDATE barang SET NmBrg='$nama', Harga='$harga' WHERE KdBrg='$kode'");
}
#akhir tombol simpan diklik
?>