-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_menu.php
More file actions
209 lines (166 loc) · 7.71 KB
/
edit_menu.php
File metadata and controls
209 lines (166 loc) · 7.71 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
session_start();
// cek apakah yang mengakses halaman ini sudah login
if ($_SESSION['level'] == "") {
header("location:login.php?pesan=gagal");
}
?>
<?php
include('koneksi.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['edit_menu'])) {
$menuId = $_POST["id"];
$name = $_POST["name"];
$price = $_POST["price"];
$category = $_POST["category"];
$details = $_POST["details"];
// File upload path
$targetDir = "uploads/";
$imageName = uniqid() . '_' . basename($_FILES["image"]["name"]);
$image = $targetDir . $imageName;
// Check if a new image file is uploaded
if ($_FILES["image"]["error"] === UPLOAD_ERR_OK) {
move_uploaded_file($_FILES["image"]["tmp_name"], $image);
// Remove "uploads/" prefix from the image path
$image = str_replace('uploads/', '', $image);
} else {
// If no new image is uploaded, use the existing image
$query = "SELECT image FROM menus WHERE no_menu = ?";
$stmt = $koneksi->prepare($query);
if ($stmt) {
$stmt->bind_param("i", $menuId);
$stmt->execute();
$stmt->bind_result($existingImage);
$stmt->fetch();
// Remove "uploads/" prefix from the existing image path, if present
$existingImage = str_replace('uploads/', '', $existingImage);
$image = $existingImage;
$stmt->close();
} else {
echo "Error retrieving existing image from the database: " . $koneksi->error;
exit;
}
}
// Update data in the database
$updateQuery = "UPDATE menus SET name = ?, price = ?, category = ?, image = ?, details = ? WHERE no_menu = ?";
$updateStmt = $koneksi->prepare($updateQuery);
if ($updateStmt) {
$updateStmt->bind_param("sdssbi", $name, $price, $category, $image, $details, $menuId);
$updateStmt->execute();
if ($updateStmt->error) {
echo "Error executing the update statement: " . $updateStmt->error;
} else {
$updateStmt->close();
// Set 'success' flag in the URL
header("location: add_menu.php?success=2");
exit;
}
} else {
echo "Error preparing the update statement: " . $koneksi->error;
}
}
}
?>
<!-- HTML form for editing menu details -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Edit Menu</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Josefin+Sans:400,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Great+Vibes" rel="stylesheet">
<link rel="stylesheet" href="css/open-iconic-bootstrap.min.css">
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<link rel="stylesheet" href="css/magnific-popup.css">
<link rel="stylesheet" href="css/aos.css">
<link rel="stylesheet" href="css/ionicons.min.css">
<link rel="stylesheet" href="css/bootstrap-datepicker.css">
<link rel="stylesheet" href="css/jquery.timepicker.css">
<link rel="stylesheet" href="css/flaticon.css">
<link rel="stylesheet" href="css/icomoon.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php include 'header_admin.php'; ?>
<?php
// Ambil ID dari parameter URL
$menuId = $_GET['id'];
// Query untuk mendapatkan detail menu berdasarkan ID
$query = "SELECT * FROM menus WHERE no_menu = ?";
$stmt = $koneksi->prepare($query);
if ($stmt) {
$stmt->bind_param("i", $menuId);
$stmt->execute();
$result = $stmt->get_result();
$menu = $result->fetch_assoc();
$stmt->close();
} else {
echo "Error preparing statement: " . $koneksi->error;
}
?>
<br><br><br>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<h2 class="text-center">Edit Menu</h2>
<form action="edit_menu.php?id=<?php echo $menu['no_menu']; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $menu['no_menu']; ?>">
<!-- Form fields untuk edit -->
<div class="form-group">
<label for="name">Nama Menu:</label>
<input type="text" name="name" class="form-control" required value="<?php echo $menu['name']; ?>">
</div>
<div class="form-group">
<label for="price">Harga:</label>
<input type="number" name="price" step="1" class="form-control" required value="<?php echo $menu['price']; ?>">
</div>
<div class="form-group">
<label for="category">Kategori:</label>
<select name="category" class="form-control" required>
<option value="minuman" <?php echo ($menu['category'] == 'minuman') ? 'selected' : ''; ?>>Minuman</option>
<option value="makanan" <?php echo ($menu['category'] == 'makanan') ? 'selected' : ''; ?>>Makanan</option>
</select>
</div>
<div class="form-group">
<label for="image">Gambar:</label>
<input type="file" name="image" class="form-control-file" accept="image/*">
</div>
<div class="form-group">
<label for="details">Deskripsi:</label>
<textarea name="details" class="form-control" rows="4" cols="50" required><?php echo $menu['details']; ?></textarea>
</div>
<!-- Akhir Form fields untuk edit -->
<button type="submit" name="edit_menu" class="btn btn-primary">Update Menu</button>
</form>
</div>
</div>
</div>
<br><br><br>
<!-- loader -->
<div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px">
<circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee" />
<circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#F96D00" />
</svg></div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery-migrate-3.0.1.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/jquery.waypoints.min.js"></script>
<script src="js/jquery.stellar.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/jquery.magnific-popup.min.js"></script>
<script src="js/aos.js"></script>
<script src="js/jquery.animateNumber.min.js"></script>
<script src="js/bootstrap-datepicker.js"></script>
<script src="js/jquery.timepicker.min.js"></script>
<script src="js/scrollax.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBVWaKrjvy3MaE7SQ74_uJiULgl1JY0H2s&sensor=false"></script>
<script src="js/google-map.js"></script>
<script src="js/main.js"></script>
</body>
</html>