Nomor 1
Tampil Data Barang
<?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>
Ubah Data Barang
<?php
if(isset($_POST['btnUbah'])){
$KdBrg = $_POST['KdBrg'];
$NmBrg = $_POST['NmBrg'];
$Harga = $_POST['Harga'];
if(empty($KdBrg)|| empty($NmBrg)){
echo '<div class="warning">Data tidak boleh kosong</div>';
}else{
$edit=mysqli_query($koneksi, "UPDATE barang SET NmBrg='$NmBrg',Harga='$Harga' WHERE KdBrg='$KdBrg'");
if($edit){
echo '<div class="success">berhasil diedit</div>';
}else{
echo '<div class="error">gagal diedit</div>';
}
}
}
$kode = $_GET['kode'];
$sql = mysqli_query($koneksi,"SELECT * FROM barang WHERE KdBrg = '$kode'");
$data = mysqli_fetch_array($sql);
?>
<form method="POST" >
<h1>Ubah Data Barang</h1>
<table>
<tr>
<td>Kode Barang :</td>
<td><input type="text" name="KdBrg" value="<?php echo $data['KdBrg'];?>" ></td>
</tr>
<tr>
<td>Nama Barang :</td>
<td><input type="text" name="NmBrg" value="<?php echo $data['NmBrg'];?>"></td>
</tr>
<tr>
<td>Harga :</td>
<td><input type="text" name="Harga" onkeyup="this.value=this.value.replace(/[^\d]/,'')" value="<?php echo $data['Harga'];?>"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Ubah" name="btnUbah">
</td>
</tr>
</table>
</form>