Nomor 1

Jawaban soal nomor 1:


  • Masukkan 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>


  • Copy file entry.php kemudian rename menjadi file ubah.php, masukkan kode berikut :

<?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>

<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="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"];

#coba cek ada gak isinya

echo $kode . $nama . $harga;

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

} #akhir tombol ubah diklik

?>