-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_edit.php
More file actions
116 lines (107 loc) · 5.13 KB
/
shape_edit.php
File metadata and controls
116 lines (107 loc) · 5.13 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
<?php
// shape_edit.php
include 'header.php';
include 'sidebar.php';
// Prevent customers from accessing write endpoints
if (isset($_SESSION['role']) && $_SESSION['role'] === 'customer') {
header('HTTP/1.1 403 Forbidden');
echo 'Forbidden';
exit;
}
$shape_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$update_message = '';
// Fetch the current shape data first
$shape = null;
if ($shape_id > 0) {
$stmt = $conn->prepare("SELECT * FROM shapes WHERE id = ?");
$stmt->bind_param("i", $shape_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
$shape = $result->fetch_assoc();
}
$stmt->close();
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['shape_id'])) {
$shape_id = intval($_POST['shape_id']);
$shape_name = trim($_POST['shape_name']);
$status = trim($_POST['status']);
$image_path = $shape['image']; // Keep old image by default
// --- New Image Upload Logic ---
if (isset($_FILES["image"]) && $_FILES["image"]["error"] == 0) {
// First, delete the old image if it exists
if (!empty($image_path) && file_exists($image_path)) {
unlink($image_path);
}
$target_dir = "assets/images/shapes/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$image_path = $target_file;
} else {
$update_message = "<div class='bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded'>Sorry, there was an error uploading your new file.</div>";
}
}
if (empty($update_message)) {
$sql = "UPDATE shapes SET shape_name=?, image=?, status=? WHERE id=?";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("sssi", $shape_name, $image_path, $status, $shape_id);
if ($stmt->execute()) {
$update_message = "<div class='bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded'>Shape updated successfully!</div>";
// Refresh shape data to show new image
$shape['image'] = $image_path;
} else {
$update_message = "<div class='bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded'>Error: " . $stmt->error . "</div>";
}
$stmt->close();
}
}
}
$conn->close();
?>
<!-- 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 Shape</h4>
<?php echo $update_message; ?>
<?php if ($shape): ?>
<form action="shape_edit.php?id=<?php echo $shape_id; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="shape_id" value="<?php echo $shape['id']; ?>">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label class="block text-gray-700 text-sm font-bold mb-2">Shape Name</label>
<input type="text" name="shape_name" required
value="<?php echo htmlspecialchars($shape['shape_name']); ?>"
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">Status</label>
<select name="status" class="shadow border rounded w-full py-2 px-3 text-gray-700">
<option value="Enable" <?php echo ($shape['status'] == 'Enable') ? 'selected' : ''; ?>>Enable</option>
<option value="Disable" <?php echo ($shape['status'] == 'Disable') ? 'selected' : ''; ?>>Disable
</option>
</select>
</div>
<div class="md:col-span-2">
<label class="block text-gray-700 text-sm font-bold mb-2">Current Image</label>
<?php if (!empty($shape['image']) && file_exists($shape['image'])): ?>
<img src="<?php echo htmlspecialchars($shape['image']); ?>" alt="Current Image"
class="h-24 w-24 object-contain border p-1 rounded mb-4">
<?php else: ?>
<p class="text-gray-500">No image uploaded.</p>
<?php endif; ?>
<label class="block text-gray-700 text-sm font-bold mb-2">Upload New Image (optional)</label>
<input type="file" name="image" accept="image/*"
class="shadow border rounded w-full py-2 px-3 text-gray-700">
</div>
</div>
<div class="mt-8 flex justify-end">
<a href="shape_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">Shape not found.</p>
<?php endif; ?>
</div>
<?php include 'footer.php'; ?>