Advertisement
Rahardyan

edit

Nov 22nd, 2024
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.80 KB | None | 0 0
  1. <?php
  2. require 'db.php';
  3.  
  4. // Validasi dan sanitasi input ID dari URL
  5. $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
  6. if ($id <= 0) {
  7.     die("ID tidak valid.");
  8. }
  9.  
  10. // Ambil data barang berdasarkan ID menggunakan prepared statement
  11. $query = "SELECT * FROM items WHERE id = ?";
  12. $stmt = mysqli_prepare($conn, $query);
  13. mysqli_stmt_bind_param($stmt, "i", $id);
  14. mysqli_stmt_execute($stmt);
  15. $result = mysqli_stmt_get_result($stmt);
  16. $item = mysqli_fetch_assoc($result);
  17. mysqli_stmt_close($stmt);
  18.  
  19. if (!$item) {
  20.     die("Barang tidak ditemukan.");
  21. }
  22.  
  23. // Ambil data kategori
  24. $categories_query = "SELECT * FROM categories";
  25. $categories = mysqli_query($conn, $categories_query);
  26.  
  27. // Proses saat form disubmit
  28. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  29.     // Validasi input
  30.     $name = trim($_POST['name']);
  31.     $category_id = !empty($_POST['category_id']) ? (int)$_POST['category_id'] : null;
  32.     $quantity = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 0;
  33.     $price = isset($_POST['price']) ? (float)$_POST['price'] : 0.0;
  34.  
  35.     // Validasi data yang wajib diisi
  36.     if (empty($name)) {
  37.         die("Nama barang tidak boleh kosong.");
  38.     }
  39.  
  40.     if ($quantity <= 0) {
  41.         die("Jumlah harus lebih besar dari 0.");
  42.     }
  43.  
  44.     if ($price <= 0) {
  45.         die("Harga harus lebih besar dari 0.");
  46.     }
  47.  
  48.     // Update data barang menggunakan prepared statement
  49.     $update_query = "UPDATE items SET name = ?, category_id = ?, quantity = ?, price = ? WHERE id = ?";
  50.     $update_stmt = mysqli_prepare($conn, $update_query);
  51.     mysqli_stmt_bind_param($update_stmt, "siidi", $name, $category_id, $quantity, $price, $id);
  52.  
  53.     if (mysqli_stmt_execute($update_stmt)) {
  54.         header("Location: index.php");
  55.         exit;
  56.     } else {
  57.         echo "Gagal mengedit barang: " . mysqli_error($conn);
  58.     }
  59.  
  60.     mysqli_stmt_close($update_stmt);
  61. }
  62.  
  63. // Tutup koneksi
  64. mysqli_close($conn);
  65. ?>
  66.  
  67. <!DOCTYPE html>
  68. <html lang="id">
  69. <head>
  70.     <meta charset="UTF-8">
  71.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  72.     <title>Edit Barang</title>
  73.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  74. </head>
  75. <body>
  76. <div class="container mt-4">
  77.     <h1 class="mb-4">Edit Barang</h1>
  78.     <form method="post">
  79.         <div class="form-group">
  80.             <label for="name">Nama Barang:</label>
  81.             <input type="text" class="form-control" id="name" name="name"
  82.                    value="<?= htmlspecialchars($item['name']) ?>" required>
  83.         </div>
  84.         <div class="form-group">
  85.             <label for="category_id">Kategori:</label>
  86.             <select class="form-control" id="category_id" name="category_id">
  87.                 <option value="">Pilih Kategori</option>
  88.                 <?php while ($row = mysqli_fetch_assoc($categories)): ?>
  89.                     <option value="<?= htmlspecialchars($row['id']) ?>"
  90.                         <?= $row['id'] == $item['category_id'] ? 'selected' : '' ?>>
  91.                         <?= htmlspecialchars($row['name']) ?>
  92.                     </option>
  93.                 <?php endwhile; ?>
  94.             </select>
  95.         </div>
  96.         <div class="form-group">
  97.             <label for="quantity">Jumlah:</label>
  98.             <input type="number" class="form-control" id="quantity" name="quantity"
  99.                    value="<?= htmlspecialchars($item['quantity']) ?>" min="1" required>
  100.         </div>
  101.         <div class="form-group">
  102.             <label for="price">Harga:</label>
  103.             <input type="number" class="form-control" id="price" name="price"
  104.                    value="<?= htmlspecialchars($item['price']) ?>" step="0.01" min="0.01" required>
  105.         </div>
  106.         <button type="submit" class="btn btn-primary">Simpan</button>
  107.     </form>
  108. </div>
  109. </body>
  110. </html>
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement