Advertisement
Rahardyan

Create

Nov 22nd, 2024 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php
  2. require 'db.php';
  3.  
  4. // Ambil kategori untuk dropdown
  5. $query = "SELECT * FROM categories";
  6. $categories = mysqli_query($conn, $query);
  7.  
  8. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  9.     // Validasi input
  10.     $name = trim($_POST['name']);
  11.     $category_id = !empty($_POST['category_id']) ? (int)$_POST['category_id'] : null;
  12.     $quantity = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 0;
  13.     $price = isset($_POST['price']) ? (float)$_POST['price'] : 0.0;
  14.  
  15.     // Pastikan input tidak kosong
  16.     if (empty($name)) {
  17.         echo "Nama barang tidak boleh kosong.";
  18.         exit;
  19.     }
  20.  
  21.     if ($quantity <= 0) {
  22.         echo "Jumlah harus lebih besar dari 0.";
  23.         exit;
  24.     }
  25.  
  26.     if ($price <= 0) {
  27.         echo "Harga harus lebih besar dari 0.";
  28.         exit;
  29.     }
  30.  
  31.     // Siapkan query dengan prepared statements
  32.     $query = "INSERT INTO items (name, category_id, quantity, price) VALUES (?, ?, ?, ?)";
  33.     $stmt = mysqli_prepare($conn, $query);
  34.  
  35.     // Bind parameter
  36.     mysqli_stmt_bind_param($stmt, "siid", $name, $category_id, $quantity, $price);
  37.  
  38.     // Eksekusi query
  39.     if (mysqli_stmt_execute($stmt)) {
  40.         header('Location: index.php');
  41.         exit;
  42.     } else {
  43.         echo 'Gagal menambahkan barang: ' . mysqli_error($conn);
  44.     }
  45.  
  46.     // Tutup statement
  47.     mysqli_stmt_close($stmt);
  48. }
  49.  
  50. // Tutup koneksi
  51. mysqli_close($conn);
  52. ?>
  53.  
  54. <!DOCTYPE html>
  55. <html lang="id">
  56. <head>
  57.     <meta charset="UTF-8">
  58.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  59.     <title>Tambah Barang</title>
  60.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  61. </head>
  62. <body>
  63. <div class="container mt-4">
  64.     <h1 class="mb-4">Tambah Barang</h1>
  65.     <form method="post">
  66.         <div class="form-group">
  67.             <label for="name">Nama Barang:</label>
  68.             <input type="text" class="form-control" id="name" name="name" required>
  69.         </div>
  70.         <div class="form-group">
  71.             <label for="category_id">Kategori:</label>
  72.             <select class="form-control" id="category_id" name="category_id">
  73.                 <option value="">Pilih Kategori</option>
  74.                 <?php while ($row = mysqli_fetch_assoc($categories)): ?>
  75.                     <option value="<?= htmlspecialchars($row['id']) ?>">
  76.                         <?= htmlspecialchars($row['name']) ?>
  77.                     </option>
  78.                 <?php endwhile; ?>
  79.             </select>
  80.         </div>
  81.         <div class="form-group">
  82.             <label for="quantity">Jumlah:</label>
  83.             <input type="number" class="form-control" id="quantity" name="quantity" min="1" required>
  84.         </div>
  85.         <div class="form-group">
  86.             <label for="price">Harga:</label>
  87.             <input type="number" class="form-control" id="price" name="price" step="0.01" min="0.01" required>
  88.         </div>
  89.         <button type="submit" class="btn btn-primary">Simpan</button>
  90.     </form>
  91. </div>
  92. </body>
  93. </html>
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement