Nomor 1
Jawaban soal nomor 1:
Tempelkan kode berikut pada file Tampil.php :
<?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>
Tempelkan kode berikut pada file Ubah.php :
<?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><input type="text" name="txtKd" disabled='disabled' value="<?php echo $data['KdBrg']; ?>"></td>
</tr>
<tr>
<td>Nama Barang</td>
<td><input type="text" name="txtNm" value="<?php echo $data['NmBrg']; ?>"></td>
</tr>
<tr>
<td>Harga</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"]))
{
$kode = $_POST["txtKd"];
$nama = $_POST["txtNm"];
$harga = $_POST["txtHr"];
$query = $koneksi->query("UPDATE barang SET NmBrg='$nama', Harga='$harga' WHERE KdBrg='$kode'");
}
?>