-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiamond_edit.php
More file actions
227 lines (208 loc) · 11.5 KB
/
diamond_edit.php
File metadata and controls
227 lines (208 loc) · 11.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
// diamond_edit.php
include 'header.php';
include 'sidebar.php';
$diamond_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$update_message = '';
// Create uploads directories if they don't exist
$cert_dir = 'uploads/certificates/';
$image_dir = 'uploads/diamonds/';
if (!file_exists($image_dir)) {
mkdir($image_dir, 0777, true);
}
// Handle form submission for updating
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['diamond_id'])) {
$diamond_id = intval($_POST['diamond_id']);
// Collect and sanitize input
$stock_no = trim($_POST['stock_no']);
$shape = trim($_POST['shape']);
$size = trim($_POST['size']);
$color = trim($_POST['color']);
$clarity = trim($_POST['clarity']);
$cut = trim($_POST['cut']);
$polish = trim($_POST['polish']);
$symmetry = trim($_POST['symmetry']);
$diamond_type = trim($_POST['diamond_type']);
// Handle diamond image upload
$image_url = null;
if (isset($_FILES['diamond_image']) && $_FILES['diamond_image']['error'] == 0) {
$allowed_image_types = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
$image_type = $_FILES['diamond_image']['type'];
if (in_array($image_type, $allowed_image_types)) {
$image_extension = pathinfo($_FILES['diamond_image']['name'], PATHINFO_EXTENSION);
$new_image_name = 'diamond_' . time() . '_' . uniqid() . '.' . $image_extension;
$image_upload_path = $image_dir . $new_image_name;
if (move_uploaded_file($_FILES['diamond_image']['tmp_name'], $image_upload_path)) {
$image_url = $image_upload_path;
}
}
}
// Build SQL based on whether we have a new image
if ($image_url) {
$sql = "UPDATE diamonds SET stock_no=?, shape=?, size=?, color=?, clarity=?, cut=?, polish=?, symmetry=?, diamond_type=?, image_url=? WHERE id=?";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ssdsssssssi", $stock_no, $shape, $size, $color, $clarity, $cut, $polish, $symmetry, $diamond_type, $image_url, $diamond_id);
}
} else {
$sql = "UPDATE diamonds SET stock_no=?, shape=?, size=?, color=?, clarity=?, cut=?, polish=?, symmetry=?, diamond_type=? WHERE id=?";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ssdssssssi", $stock_no, $shape, $size, $color, $clarity, $cut, $polish, $symmetry, $diamond_type, $diamond_id);
}
}
if (isset($stmt)) {
if ($stmt->execute()) {
$update_message = "<div class='bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4'>
<strong>Success!</strong> Diamond details updated successfully" . ($image_url ? " with new image" : "") . "!
</div>";
} else {
$update_message = "<div class='bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4'>Error: " . $stmt->error . "</div>";
}
$stmt->close();
}
}
// Fetch the current diamond data
$diamond = null;
if ($diamond_id > 0) {
$stmt = $conn->prepare("SELECT * FROM diamonds WHERE id = ?");
$stmt->bind_param("i", $diamond_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
$diamond = $result->fetch_assoc();
}
$stmt->close();
}
// Fetch data for dropdowns
$shapes = $conn->query("SELECT shape_name FROM shapes WHERE status='Enable' ORDER BY shape_name");
$colors = $conn->query("SELECT color_name FROM colors WHERE status='Enable' ORDER BY color_name");
$clarities = $conn->query("SELECT clarity_name FROM clarities WHERE status='Enable' ORDER BY clarity_name");
$cuts = $conn->query("SELECT cut_name FROM cuts WHERE status='Enable' ORDER BY cut_name");
$polishes = $conn->query("SELECT polish_name FROM polishes WHERE status='Enable' ORDER BY polish_name");
?>
<!-- Main Content -->
<div class="bg-white p-8 rounded-lg shadow-md w-full">
<h4 class="text-2xl font-bold text-gray-700 mb-6">Edit Diamond</h4>
<?php echo $update_message; ?>
<?php if ($diamond): ?>
<form action="diamond_edit.php?id=<?php echo $diamond_id; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="diamond_id" value="<?php echo $diamond['id']; ?>">
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
<!-- Form fields pre-filled with diamond data -->
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Stock No</label>
<input type="text" name="stock_no" required
value="<?php echo htmlspecialchars($diamond['stock_no']); ?>"
class="shadow border rounded w-full py-2 px-3 text-gray-700">
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Shape</label>
<select name="shape" class="shadow border rounded w-full py-2 px-3 text-gray-700">
<?php while ($row = $shapes->fetch_assoc()): ?>
<option value="<?php echo $row['shape_name']; ?>" <?php echo ($diamond['shape'] == $row['shape_name']) ? 'selected' : ''; ?>>
<?php echo $row['shape_name']; ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Size</label>
<input type="number" step="0.01" name="size" value="<?php echo htmlspecialchars($diamond['size']); ?>"
class="shadow border rounded w-full py-2 px-3 text-gray-700">
</div>
<!-- Other fields follow the same pattern -->
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Color</label>
<select name="color" class="shadow border rounded w-full py-2 px-3 text-gray-700">
<?php while ($row = $colors->fetch_assoc()): ?>
<option value="<?php echo $row['color_name']; ?>" <?php echo ($diamond['color'] == $row['color_name']) ? 'selected' : ''; ?>>
<?php echo $row['color_name']; ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Clarity</label>
<select name="clarity" class="shadow border rounded w-full py-2 px-3 text-gray-700">
<?php while ($row = $clarities->fetch_assoc()): ?>
<option value="<?php echo $row['clarity_name']; ?>" <?php echo ($diamond['clarity'] == $row['clarity_name']) ? 'selected' : ''; ?>>
<?php echo $row['clarity_name']; ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Cut</label>
<select name="cut" class="shadow border rounded w-full py-2 px-3 text-gray-700">
<?php while ($row = $cuts->fetch_assoc()): ?>
<option value="<?php echo $row['cut_name']; ?>" <?php echo ($diamond['cut'] == $row['cut_name']) ? 'selected' : ''; ?>>
<?php echo $row['cut_name']; ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Polish</label>
<select name="polish" class="shadow border rounded w-full py-2 px-3 text-gray-700">
<?php while ($row = $polishes->fetch_assoc()): ?>
<option value="<?php echo $row['polish_name']; ?>" <?php echo ($diamond['polish'] == $row['polish_name']) ? 'selected' : ''; ?>>
<?php echo $row['polish_name']; ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Symmetry</label>
<input type="text" name="symmetry" value="<?php echo htmlspecialchars($diamond['symmetry']); ?>"
class="shadow border rounded w-full py-2 px-3 text-gray-700">
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Diamond Type</label>
<input type="text" name="diamond_type" value="<?php echo htmlspecialchars($diamond['diamond_type']); ?>"
class="shadow border rounded w-full py-2 px-3 text-gray-700">
</div>
</div>
<!-- Current Image Display and Upload -->
<div class="mt-6 p-6 bg-gradient-to-r from-blue-50 to-indigo-50 rounded-lg border-2 border-blue-200">
<div class="flex items-center mb-4">
<i class="fas fa-image text-2xl text-blue-600 mr-3"></i>
<h5 class="text-lg font-bold text-gray-700">Diamond Image</h5>
</div>
<?php if (!empty($diamond['image_url']) && file_exists($diamond['image_url'])): ?>
<div class="mb-4">
<p class="text-sm text-gray-700 font-semibold mb-2">Current Image:</p>
<img src="<?php echo htmlspecialchars($diamond['image_url']); ?>" alt="Diamond"
class="w-48 h-48 object-cover rounded-lg shadow-md border-2 border-blue-300">
</div>
<?php endif; ?>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">
<?php echo (!empty($diamond['image_url']) && file_exists($diamond['image_url'])) ? 'Replace Image' : 'Upload Image'; ?>
(JPG, PNG, or WebP)
<span class="text-gray-500 font-normal italic">- Optional</span>
</label>
<input type="file" name="diamond_image" accept=".jpg,.jpeg,.png,.webp"
class="shadow border rounded w-full py-2 px-3 text-gray-700 bg-white file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
<p class="text-xs text-gray-500 mt-2">
<i class="fas fa-info-circle mr-1"></i>
Upload a new image to replace the current one. Leave empty to keep existing image.
</p>
</div>
</div>
<div class="mt-8 flex justify-end">
<a href="diamond_master.php"
class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded mr-2">
Back to List
</a>
<button type="submit" class="bg-pink-600 hover:bg-pink-700 text-white font-bold py-2 px-4 rounded">
Save Changes
</button>
</div>
</form>
<?php else: ?>
<p class="text-red-500">Diamond not found.</p>
<?php endif; ?>
</div>
<?php
$conn->close();
include 'footer.php';
?>