Management stok database SQL di localhost (Tambah barang dan liat daftar barang) #1

Index.php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Management Produk</title>
    <link rel="stylesheet" href="style.css">
    <?php
    require 'koneksi.php';
    ?>
</head>

<body>
    <header>
        <h1>Management Produk</h1>
    </header>
    <main>
        <br><br><br>
        <section class="form">
            <h2>Tambah Produk</h2>
            <form action="index.php" method="POST">
                <input type="text" placeholder="Tipe" name="tipe">
                <input type="text" placeholder="Nama" name="nama">
                <input type="text" placeholder="Tipe" name="deskripsi">
                <input type="text" placeholder="Tipe" name="harga">
                <input type="text" placeholder="Stok" name="stok">
                <button type="submit">Tambah</button>
            </form>


            <?php

            if ($_SERVER["REQUEST_METHOD"] === "POST") {
                // Dapatkan data dari form
                $tipe = $_POST["tipe"];
                $nama = $_POST["nama"];
                $deskripsi = $_POST["deskripsi"];
                $harga = $_POST["harga"];
                $stok = $_POST["stok"];

                if (isset($tipe) && isset($nama) && isset($deskripsi) && isset($harga) && isset($stok)) {
                    // Buat query SQL untuk menambahkan data
                    $sql = "INSERT INTO `barang`(`tipe`, `nama`, `deskripsi`, `harga`, `stok`) VALUES ('$tipe','$nama','$deskripsi','$harga','$stok')";

                    // Jalankan query
                    if (mysqli_query($conn, $sql)) {
                        echo "Data barang berhasil ditambahkan.";
                    } else {
                        echo "Gagal menambahkan data barang: " . mysqli_error($conn);
                    }
                }
            }

            // Tutup koneksi datab
            ?>
        </section>
        <br><br><br>
        <section class="products">
            <h2>Produk</h2>
            <table>
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Nama</th>
                        <th>Tipe</th>
                        <th>Deskripsi</th>
                        <th>Harga</th>
                        <th>Stok</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    // Query untuk mengambil data
                    $sql = "SELECT * FROM barang";
                    $result = mysqli_query($conn, $sql);
                    $i = 0;

                    // Looping untuk setiap baris data
                    while ($row = mysqli_fetch_assoc($result)) {
                        $i++;
                    ?>
                        <tr>
                            <td><?php echo $i; ?></td>
                            <td><?php echo $row["nama"]; ?></td>
                            <td><?php echo $row["tipe"]; ?></td>
                            <td><?php echo $row["deskripsi"]; ?></td>
                            <td><?php echo $row["harga"]; ?></td>
                            <td><?php echo $row["stok"]; ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </section>
        <br><br><br>
    </main>
    <footer>
        &copy; 2024
    </footer>
</body>

</html>


koneksi.php

<?php

// Deklarasi variabel untuk koneksi database
$servername = "localhost";
$username = "root";
$password = "";
$database = "toko";

// Buat koneksi ke database MySQL
$conn = mysqli_connect($servername, $username, $password, $database);

// Periksa koneksi
if (!$conn) {
    die("Koneksi ke database gagal: " . mysqli_connect_error());
}

?>


style.css

body {
    font-family: sans-serif;
    background-color: #ffffff;
}

header {
    background-color: #000000;
    color: #ffffff;
    padding: 20px;
}

h1 {
    font-size: 20px;
    font-weight: bold;
}

main {
    margin: 0 auto;
    width: 800px;
}

.products {
    border: 1px solid #000000;
    padding: 20px;
}

table {
    width: 100%;
}

th,
td {
    border: 1px solid #000000;
    padding: 10px;
}

.form {
    margin-top: 20px;
}

form {
    width: 500px;
}

input {
    width: 100%;
    padding: 10px;
    border: 1px solid #000000;
}

button {
    background-color: #000000;
    color: #ffffff;
    padding: 10px;
    border: none;
    cursor: pointer;
}

footer {
    text-align: center;
}

Komentar