1 | <?php
|
---|
2 |
|
---|
3 | session_start();
|
---|
4 |
|
---|
5 | if(!isset($_SESSION['user_ID']) || !isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
|
---|
6 | header("Location: ./Log In.php");
|
---|
7 | }
|
---|
8 |
|
---|
9 | require 'connect.php';
|
---|
10 |
|
---|
11 | if (isset($_POST['update'])) {
|
---|
12 |
|
---|
13 | $id = $_POST['product_id'];
|
---|
14 | $name = $_POST['productName'];
|
---|
15 | $price = $_POST['productPrice'];
|
---|
16 | $discount = $_POST['productDiscount'];
|
---|
17 | $instock = $_POST['inStock'];
|
---|
18 | $category = $_POST['productCategory'];
|
---|
19 | $description = $_POST['productDesc'];
|
---|
20 |
|
---|
21 | $stmt = $conn->prepare("SELECT * FROM products WHERE product_id = ?");
|
---|
22 | $stmt->bind_param("i", $id);
|
---|
23 | $stmt->execute();
|
---|
24 | $result = $stmt->get_result();
|
---|
25 | $row = $result->fetch_assoc();
|
---|
26 |
|
---|
27 | $images = $_FILES['images'] ?? [];
|
---|
28 | $targetDirectory = "./UPLOADED_IMAGES/";
|
---|
29 | $images_array[] = [];
|
---|
30 |
|
---|
31 |
|
---|
32 | if (!empty($images)) {
|
---|
33 | foreach ($_FILES['images']['name'] as $key => $file_name) {
|
---|
34 | $tmpName = $_FILES['images']['tmp_name'][$key];
|
---|
35 | $fileName = basename($file_name);
|
---|
36 |
|
---|
37 | $targetFilePath = $targetDirectory.$fileName;
|
---|
38 | if (move_uploaded_file($tmpName, $targetFilePath)) {
|
---|
39 | echo "File $fileName uploaded successfully.<br>";
|
---|
40 | $images_array[] = $targetFilePath;
|
---|
41 | } else {
|
---|
42 | $images_array[] = null;
|
---|
43 | echo "Failed to upload $fileName.<br>";
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | echo "<br></br>";
|
---|
49 | print_r($images_array);
|
---|
50 | echo "<br></br>";
|
---|
51 |
|
---|
52 | $brand = $_POST['brand-value'];
|
---|
53 | $series = $_POST['series-value'];
|
---|
54 | $modelNumber = $_POST['modelnumber-value'];
|
---|
55 | $weight = $_POST['weight-value'];
|
---|
56 | $dimensions = $_POST['dimensions-value'];
|
---|
57 | $color = $_POST['color-value'];
|
---|
58 | $manufactuer = $_POST['manufactuer-value'];
|
---|
59 | $origin = $_POST['origin-value'];
|
---|
60 | $firstDate = $_POST['firstDate-value'];
|
---|
61 |
|
---|
62 | $energy = $_POST['energy-value'] ?? '';
|
---|
63 | $volts = $_POST['volts-value'] ?? '';
|
---|
64 | $memorySpeed = $_POST['memorySpeed-value'] ?? '';
|
---|
65 | $usb3 = $_POST['usb3-value'] ?? '';
|
---|
66 | $usb2 = $_POST['usb2-value'] ?? '';
|
---|
67 | $processorBrand = $_POST['processorBrand-value'] ?? '';
|
---|
68 | $numProcess = $_POST['numProcess-value'] ?? NULL;
|
---|
69 | $typeMem = $_POST['typeMem-value'] ?? '';
|
---|
70 | $HardDD = $_POST['HardDD-value'] ?? '';
|
---|
71 | $HardPlat = $_POST['HardPlat-value'] ?? '';
|
---|
72 | $HardFlash = $_POST['HardFlash-value'] ?? '';
|
---|
73 | $HardInterface = $_POST['HardInterface-value'] ?? NULL;
|
---|
74 | $gpuRAM = $_POST['gpuRAM-value'] ?? NULL;
|
---|
75 | $motherboardRAM = $_POST['motherboardRAM-value'] ?? NULL;
|
---|
76 | $motherboardTYPE = $_POST['motherboardTYPE-value'] ?? NULL;
|
---|
77 | $screenSize = $_POST['screenSize-value'] ?? '';
|
---|
78 | $screenRes = $_POST['screenRes-value'] ?? '';
|
---|
79 | $screenMax = $_POST['screenMax-value'] ?? '';
|
---|
80 |
|
---|
81 |
|
---|
82 | if(empty($gpuRAM)) {
|
---|
83 | $gpuRAM = NULL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if(empty($motherboardRAM)) {
|
---|
87 | $motherboardRAM = NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if(empty($numProcess)) {
|
---|
91 | $numProcess = NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if(empty($memorySpeed)) {
|
---|
95 | $memorySpeed = NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | $image1 = isset($images_array[1]) ? $images_array[1] : $row['image1'];
|
---|
99 | $image2 = isset($images_array[2]) ? $images_array[2] : $row['image2'];
|
---|
100 | $image3 = isset($images_array[3]) ? $images_array[3] : $row['image3'];
|
---|
101 | $image4 = isset($images_array[4]) ? $images_array[4] : $row['image4'];
|
---|
102 |
|
---|
103 |
|
---|
104 | if (empty($name) || empty($price) || empty($discount)) {
|
---|
105 | if (empty($name)) {
|
---|
106 | echo "<font color='red'>name field is empty.</font><br/>";
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (empty($price)) {
|
---|
110 | echo "<font color='red'>price field is empty.</font><br/>";
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (empty($discount)) {
|
---|
114 | echo "<font color='red'>discount field is empty.</font><br/>";
|
---|
115 | }
|
---|
116 | }
|
---|
117 | else {
|
---|
118 | $stmt = $conn->prepare("UPDATE products SET
|
---|
119 | name = ?,
|
---|
120 | price = ?,
|
---|
121 | discount = ?,
|
---|
122 | in_stock = ?,
|
---|
123 | category = ?,
|
---|
124 | description = ?,
|
---|
125 | image1 = ?,
|
---|
126 | image2 = ?,
|
---|
127 | image3 = ?,
|
---|
128 | image4 = ?,
|
---|
129 | brand = ?,
|
---|
130 | series = ?,
|
---|
131 | model_number = ?,
|
---|
132 | weight = ?,
|
---|
133 | dimensions = ?,
|
---|
134 | color = ?,
|
---|
135 | manufactuer = ?,
|
---|
136 | origin = ?,
|
---|
137 | firstDate = ?,
|
---|
138 | energy = ?,
|
---|
139 | volts = ?,
|
---|
140 | memorySpeed = ?,
|
---|
141 | usb3 = ?,
|
---|
142 | usb2 = ?,
|
---|
143 | processorBrand = ?,
|
---|
144 | numProcess = ?,
|
---|
145 | typeMem = ?,
|
---|
146 | HardDD = ?,
|
---|
147 | HardPlat = ?,
|
---|
148 | HardFlash = ?,
|
---|
149 | HardInterface = ?,
|
---|
150 | gpuRAM = ?,
|
---|
151 | motherboardRAM = ?,
|
---|
152 | motherboardTYPE = ?,
|
---|
153 | screenSize = ?,
|
---|
154 | screenRes = ?,
|
---|
155 | screenMax = ?
|
---|
156 | WHERE product_id = ?;"
|
---|
157 | );
|
---|
158 |
|
---|
159 | echo $images_array[1];
|
---|
160 |
|
---|
161 | if($instock == "true") {
|
---|
162 | $instock = 1;
|
---|
163 | }
|
---|
164 | else {
|
---|
165 | $instock = 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | $stmt->bind_param(
|
---|
169 | "ssdissssssssssssssssiiiisisssssiissssi",
|
---|
170 | $name, $price, $discount, $instock, $category, $description, $image1, $image2,
|
---|
171 | $image3, $image4, $brand, $series,
|
---|
172 | $modelNumber, $weight, $dimensions, $color, $manufactuer, $origin, $firstDate,
|
---|
173 | $energy, $volts, $memorySpeed, $usb3, $usb2, $processorBrand, $numProcess,
|
---|
174 | $typeMem, $HardDD, $HardPlat, $HardFlash, $HardInterface, $gpuRAM,
|
---|
175 | $motherboardRAM, $motherboardTYPE, $screenSize, $screenRes, $screenMax, $id);
|
---|
176 |
|
---|
177 | if ($stmt->execute()) {
|
---|
178 | echo "<p><font color='green'>Data updated successfully!</font></p>";
|
---|
179 | } else {
|
---|
180 | echo "<p><font color='red'>Error updating data: " . $conn->error . "</font></p>";
|
---|
181 | }
|
---|
182 |
|
---|
183 | $stmt->close();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | else {
|
---|
187 |
|
---|
188 | }
|
---|
189 | ?>
|
---|
190 | <p><a href="FetchData.php">View Updated Data</a></p>
|
---|