-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-menu.php
177 lines (168 loc) · 5.92 KB
/
add-menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
session_start();
include 'includes/db.php'; // Memuat koneksi database
// Cek apakah pengguna sudah login
if (!isset($_SESSION['user_email'])) {
// Jika belum login, arahkan ke halaman login
header('Location: index.html');
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$category = $_POST['category'];
$description = $_POST['description'];
$price = $_POST['price'];
$photo = $_FILES['photo'];
// Validasi dan upload gambar
if ($photo['error'] == 0) {
// Ambil ID produk terakhir untuk penamaan direktori
$result = $conn->query("SELECT MAX(id) AS max_id FROM products");
$row = $result->fetch_assoc();
$next_id = $row['max_id'] + 1;
$dir_name = 'uploads/produk_' . $next_id;
// Buat direktori baru
if (!is_dir($dir_name)) {
mkdir($dir_name, 0777, true);
}
$target_file = $dir_name . '/' . basename($photo["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Cek apakah file adalah gambar
$check = getimagesize($photo["tmp_name"]);
if ($check !== false) {
// Cek ukuran file
if ($photo["size"] <= 5000000) { // 5MB
// Hanya izinkan format tertentu
if (in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
// Pindahkan file ke folder uploads
if (move_uploaded_file($photo["tmp_name"], $target_file)) {
// Simpan informasi menu ke database
$image_url = $dir_name . '/' . basename($photo["name"]);
$sql = "INSERT INTO products (name, category, description, price, image) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $name, $category, $description, $price, $image_url);
if ($stmt->execute()) {
echo "Menu baru berhasil ditambahkan.";
header('Location: menu.php'); // Arahkan kembali ke halaman daftar menu setelah sukses menambahkan
exit();
} else {
echo "Error: " . $stmt->error;
}
} else {
echo "Maaf, terjadi kesalahan saat mengupload gambar.";
}
} else {
echo "Maaf, hanya file JPG, JPEG, PNG & GIF yang diperbolehkan.";
}
} else {
echo "Maaf, ukuran file terlalu besar.";
}
} else {
echo "File bukan gambar.";
}
} else {
echo "Tidak ada file yang diupload.";
}
// Tutup statement dan koneksi database
if (isset($stmt)) {
$stmt->close();
}
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Menu</title>
<link rel="stylesheet" href="css/styles.css">
<style>
.header-container {
background-color: #4CAF50;
color: white;
padding: 10px 0;
height: 60px; /* Atur tinggi header sesuai kebutuhan */
}
.header {
text-align: left;
font-size: 30px;
margin-bottom: 50px; /* Tambahkan margin-bottom di sini */
}
.container {
width: 50%;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 70px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
min-height: 100vh; /* agar konten bisa di-scroll ke bawah */
display: flex;
flex-direction: column;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
gap: 15px;
}
label {
font-size: 16px;
margin-bottom: 5px;
}
input[type="text"],
input[type="number"],
textarea,
select {
padding: 30px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="file"] {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
border-radius: 4px;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="header-container">
<a href="home.php" class="header" style="text-decoration: none; color: #ffffff;">Bursa Makanan</a>
</div>
<div class="container">
<h1>Tambah Menu</h1>
<form action="add-menu.php" method="POST" enctype="multipart/form-data">
<label for="photo">Foto Menu</label>
<input type="file" id="photo" name="photo" required>
<label for="name">Nama Menu</label>
<textarea id="name" name="name" required></textarea>
<label for="category">Kategori Menu</label>
<select id="category" name="category">
<option value="Roti">Roti</option>
<!-- Tambahkan opsi kategori lainnya sesuai kebutuhan -->
</select>
<label for="description">Deskripsi menu</label>
<textarea id="description" name="description" required></textarea>
<label for="price">Atur Harga (Rp)</label>
<input type="number" id="price" name="price" required>
<button type="submit">Tambahkan</button>
</form>
</div>
</body>
</html>