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 align="center">

<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" 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="2">

<input type="hidden" name="txtKd" value="<?php echo $data['KdBrg'];?>">

<input type="submit" value="Ubah" name="btnsimpan">

</td>

</tr>

</table>

</form>

<?php

if (isset($_POST["btnsimpan"]))

{

#tampung ke variabel yang isian dari form

$kode = $_POST["txtKd"];

$nama = $_POST["txtNm"];

$harga = $_POST["txtHr"];

#coba cek ada gak isinya

echo $kode . $nama . $harga;

$query = $koneksi->query("UPDATE barang SET KdBrg='$kode', NmBrg='$nama', Harga='$harga' WHERE KdBrg='$kode'");

} #akhir tombol simpan diklik

?>