PHP
PHP
Buat Database
sql
sql
USE dbbarang;
);
koneksi.php
php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "dbbarang";
if (!$conn) {
?>
style.css
css
body {
.container {
width: 80%;
margin: 0 auto;
padding: 20px;
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
table, th, td {
padding: 8px;
th {
background-color: #FA8072;
form {
margin-top: 20px;
input[type="text"], input[type="number"] {
padding: 8px;
width: 100%;
box-sizing: border-box;
input[type="submit"] {
background-color: #D30000;
color: white;
border: none;
cursor: pointer;
index.php
php
<?php
include 'koneksi.php';
// Tambah data
if (isset($_POST['submit'])) {
$kode_barang = $_POST['kode_barang'];
$nama_barang = $_POST['nama_barang'];
$harga_barang = $_POST['harga_barang'];
$sql = "INSERT INTO barang (kode_barang, nama_barang, harga_barang) VALUES ('$kode_barang',
'$nama_barang', '$harga_barang')";
mysqli_query($conn, $sql);
header("Location: index.php");
// Hapus data
if (isset($_GET['delete'])) {
$kode_barang = $_GET['delete'];
mysqli_query($conn, $sql);
header("Location: index.php");
// Edit data
if (isset($_POST['update'])) {
$kode_barang = $_POST['kode_barang'];
$nama_barang = $_POST['nama_barang'];
$harga_barang = $_POST['harga_barang'];
mysqli_query($conn, $sql);
header("Location: index.php");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Data Barang</title>
</head>
<body>
<div class="container">
<h1>Data Barang</h1>
<label>Kode Barang:</label>
<label>Nama Barang:</label>
<label>Harga Barang:</label>
</form>
<table>
<tr>
<th>Kode Barang</th>
<th>Nama Barang</th>
<th>Harga Barang</th>
<th>Aksi</th>
</tr>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
edit.php
php
<?php
include 'koneksi.php';
if (isset($_GET['kode_barang'])) {
$kode_barang = $_GET['kode_barang'];
$row = mysqli_fetch_assoc($result);
}
if (isset($_POST['update'])) {
$kode_barang = $_POST['kode_barang'];
$nama_barang = $_POST['nama_barang'];
$harga_barang = $_POST['harga_barang'];
mysqli_query($conn, $sql);
header("Location: index.php");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<label>Nama Barang:</label>
<label>Harga Barang:</label>
</form>
</div>
</body>
</html>