Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'db.php';
- $categories = mysqli_query($conn, "SELECT * FROM categories");
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $name = mysqli_real_escape_string($conn, $_POST['name']);
- $category_id = $_POST['category_id'] ?: 'NULL';
- $quantity = (int)$_POST['quantity'];
- $price = (float)$_POST['price'];
- $query = "INSERT INTO items (name, category_id, quantity, price) VALUES ('$name', $category_id, $quantity, $price)";
- if (mysqli_query($conn, $query)) {
- header('Location: index.php');
- } else {
- echo 'Gagal menambahkan barang: ' . mysqli_error($conn);
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Tambah Barang</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
- </head>
- <body>
- <h1>Tambah Barang</h1>
- <form method="post">
- <label>Nama Barang:</label>
- <input type="text" name="name" required><br>
- <label>Kategori:</label>
- <select name="category_id">
- <option value="">Pilih Kategori</option>
- <?php while ($row = mysqli_fetch_assoc($categories)): ?>
- <option value="<?= $row['id'] ?>"><?= htmlspecialchars($row['name']) ?></option>
- <?php endwhile; ?>
- </select><br>
- <label>Jumlah:</label>
- <input type="number" name="quantity" required><br>
- <label>Harga:</label>
- <input type="number" step="0.01" name="price" required><br>
- <button type="submit">Simpan</button>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement