You are on page 1of 8

INDEX

<?php

require 'config.php';

$produk = query("SELECT * FROM produk");

?>

<!DOCTYPE html>

<html>

<head>

<title>Toko Maju Jaya</title>

<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

<div class="judul">

<h1>Daftar Produk </h1>

</div>

<a href="tambah.php">+ Tambah Data Produk</a>

<br><br>

<table border="1" cellpadding="10" cellspacing="0" class="table">

<tr>

<th>No.</th>

<th>Nama Produk</th>

<th>Harga</th>

<th>Qty</th>

<th colspan="2">Aksi</th>

</tr>

<?php $i = 1; ?>

<?php foreach($produk as $row ): ?>

<tr>

<td><?= $i;?></td>

<td><?= $row['nama'] ?></td>

<td><?= $row['harga'] ?></td>

<td><?= $row['qty'] ?></td>

<td><a href="ubah.php?no=<?= $row["no"]; ?>">Ubah</a></td>

<td><a href="hapus.php?no=<?= $row["no"]; ?>" onclick="

return confirm('Anda yakin akan menghapus data ini?')">Hapus</a></td>

</tr>

<?php $i++; ?>


<?php endforeach; ?>

</table>

</body>

</html>

CONFIG
<?php

//membuat koneksi ke database

$server = "localhost";

$user = "root";

$password = "";

$db = "penjualan";

$conn = mysqli_connect($server, $user, $password, $db);

function query($query) {

global $conn;

$result = mysqli_query($conn, $query);

$rows = [];

while ($row = mysqli_fetch_assoc($result)){

$rows [] = $row;

return $rows;

function tambah($data){

global $conn;

$nama = htmlspecialchars($data["nama"]);

$harga = htmlspecialchars($data["harga"]);

$qty = htmlspecialchars($data["qty"]);
$query = "INSERT INTO produk VALUES

('','$nama','$harga','$qty')";

mysqli_query($conn, $query);

return mysqli_affected_rows($conn);

function hapus($no){

global $conn;

mysqli_query($conn, "DELETE FROM produk WHERE no=$no");

return mysqli_affected_rows($conn);

function ubah($data){

global $conn;

$no = $data["no"];

$nama = htmlspecialchars($data["nama"]);

$harga = htmlspecialchars($data["harga"]);

$qty = htmlspecialchars($data["qty"]);

$query = "UPDATE produk SET

nama = '$nama',

harga = '$harga',

qty = '$qty'

WHERE no = $no

";

mysqli_query($conn, $query);

return mysqli_affected_rows($conn);

?>

TAMBAH
<!DOCTYPE html>

<html>

<head>

<title>Tambah Data Produk</title>

</head>

<body>

<h1>Tambah Data Produk</h1>


<form action="proses_tambah.php" method="post">

<fieldset>

<p>

<label for="nama">Nama Produk: </label>

<input type="text" name="nama" id="nama">

</p>

<p>

<label for="harga">Harga : </label>

<input type="text" name="harga" id="harga">

</p>

<p>

<label for="nama">Qty : </label>

<input type="text" name="qty" id="qty">

</p>

<p>

<button type="submit" name="submit">Tambah Data</button>

</p>

</fieldset>

</form>

</body>

</html>

PROSES TAMBAH

<?php
require 'config.php';
//cek apakah tombol submit  sudah ditekan atau belum
if (isset($_POST["submit"])){
    
    //cek apakah data berhasil ditambahkan atau tidak
    if ( tambah($_POST) > 0){
        echo "
            <script>
                alert('Data Berhasil Ditambahkan');
                document.location.href = 'index.php';
            </script>
        ";
    }else{
        echo "
            <script>
                alert('Data Gagal Ditambahkan');
                document.location.href = 'index.php';
            </script>
        ";
    }
}
?>
UBAH
<?php

require 'config.php';

//ambil data diurl

$no = $_GET["no"];

//query data berdasarkan no

$produk = query("SELECT * FROM produk WHERE no = $no")[0];

//cek apakah tombol submit sudah ditekan atau belum

if (isset($_POST["submit"])){

//cek apakah data berhasil diubah atau tidak

if ( ubah($_POST) > 0){

echo "

<script>

alert('Data Berhasil Diubah');

document.location.href = 'index.php';

</script>

";

}else{

echo "

<script>

alert('Data Gagal Diubah');

document.location.href = 'index.php';

</script>

";

?>

<!DOCTYPE html>

<html>

<head>

<title>Ubah Data Produk</title>

</head>

<body>

<h1>Ubah Data Produk</h1>

<form action="" method="post">

<fieldset>

<input type="hidden" name="no" required


value="<?= $produk["no"]; ?>">

<p>

<label for="nama">Nama Produk: </label>

<input type="text" name="nama" id="nama" required

value="<?= $produk["nama"]; ?>">

</p>

<p>

<label for="harga">Harga : </label>

<input type="text" name="harga" id="harga"required

value="<?= $produk["harga"]; ?>">

</p>

<p>

<label for="nama">Qty : </label>

<input type="text" name="qty" id="qty"required

value="<?= $produk["qty"]; ?>">

</p>

<p>

<button type="submit" name="submit">Ubah Data</button>

</p>

</fieldset>

</form>

</body>

</html>

HAPUS
<?php

require 'config.php';

$no = $_GET["no"];

if ( hapus($no) > 0){

echo "

<script>

alert('Data Berhasil Dihapus');

document.location.href = 'index.php';

</script>

";

}else{

echo "
<script>

alert('Data Gagal Dihapus');

document.location.href = 'index.php';

</script>

";

?>

STYLE
body{

font-family: 'cambria';

color: #000;

.judul{

background: #87D1D8;

padding: 10px;

text-align: center;

.judul h1,h2,h3{

height: 15px;

a{

/*color: #fff;*/

padding: 5px;

text-decoration: none;

.table{

border-collapse: collapse;

table.table th th , table.table tr td{

padding: 10px 20px ;

You might also like