Advertisement
Rahardyan

Create

Nov 22nd, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2. require 'db.php';
  3.  
  4. $categories = mysqli_query($conn, "SELECT * FROM categories");
  5.  
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  7.     $name = mysqli_real_escape_string($conn, $_POST['name']);
  8.     $category_id = $_POST['category_id'] ?: 'NULL';
  9.     $quantity = (int)$_POST['quantity'];
  10.     $price = (float)$_POST['price'];
  11.  
  12.     $query = "INSERT INTO items (name, category_id, quantity, price) VALUES ('$name', $category_id, $quantity, $price)";
  13.     if (mysqli_query($conn, $query)) {
  14.         header('Location: index.php');
  15.     } else {
  16.         echo 'Gagal menambahkan barang: ' . mysqli_error($conn);
  17.     }
  18. }
  19. ?>
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23.     <title>Tambah Barang</title>
  24.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  25. </head>
  26. <body>
  27.     <h1>Tambah Barang</h1>
  28.     <form method="post">
  29.         <label>Nama Barang:</label>
  30.         <input type="text" name="name" required><br>
  31.         <label>Kategori:</label>
  32.         <select name="category_id">
  33.             <option value="">Pilih Kategori</option>
  34.             <?php while ($row = mysqli_fetch_assoc($categories)): ?>
  35.                 <option value="<?= $row['id'] ?>"><?= htmlspecialchars($row['name']) ?></option>
  36.             <?php endwhile; ?>
  37.         </select><br>
  38.         <label>Jumlah:</label>
  39.         <input type="number" name="quantity" required><br>
  40.         <label>Harga:</label>
  41.         <input type="number" step="0.01" name="price" required><br>
  42.         <button type="submit">Simpan</button>
  43.     </form>
  44. </body>
  45. </html>
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement