|
23 | 23 | $defaultStyle = $_POST["default_style"]; |
24 | 24 | $styleLocation = $_POST["default_style_location"]; |
25 | 25 | $customDetailsRequired = $_POST["customFieldRequired"]; |
26 | | - $product_id = $_SESSION["product_id"]; |
| 26 | + // prefer explicit product_id from POST, fallback to session |
| 27 | + $product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : (isset($_SESSION["product_id"]) ? intval($_SESSION["product_id"]) : 0); |
| 28 | + if ($product_id === 0) { |
| 29 | + echo json_encode(array('success' => false, 'error' => 'product_id not provided')); |
| 30 | + exit; |
| 31 | + } |
27 | 32 | $sizeAvailable = $_POST["sizeAvailable"]; |
28 | 33 |
|
29 | | - // Attempt to insert new design into table |
| 34 | + // fetch existing filenames |
| 35 | + $fstmt = $conn->prepare("SELECT filename_front, filename_back FROM products WHERE product_id = ? LIMIT 1"); |
| 36 | + $fstmt->bind_param("i", $product_id); |
| 37 | + $fstmt->execute(); |
| 38 | + $fres = $fstmt->get_result(); |
| 39 | + $existingFront = ''; |
| 40 | + $existingBack = ''; |
| 41 | + if ($fres && $fres->num_rows > 0) { |
| 42 | + $frow = $fres->fetch_assoc(); |
| 43 | + $existingFront = $frow['filename_front']; |
| 44 | + $existingBack = $frow['filename_back']; |
| 45 | + } |
| 46 | + |
| 47 | + $newFront = $existingFront; |
| 48 | + $newBack = $existingBack; |
| 49 | + |
| 50 | + // handle explicit removal flags (delete current file and clear DB filename) |
| 51 | + if (isset($_POST['remove_front']) && $_POST['remove_front'] == '1') { |
| 52 | + // Prevent deleting the side that is configured as the default style location |
| 53 | + if ($styleLocation === 'front') { |
| 54 | + echo json_encode(array('success' => false, 'error' => 'Cannot delete front design because default style location is front')); |
| 55 | + exit; |
| 56 | + } |
| 57 | + if ($existingFront && $existingFront !== '') { |
| 58 | + $oldPath = UPLOAD_DIR . $existingFront; |
| 59 | + if (file_exists($oldPath)) {@unlink($oldPath);} |
| 60 | + } |
| 61 | + $newFront = ''; |
| 62 | + } |
| 63 | + |
| 64 | + if (isset($_POST['remove_back']) && $_POST['remove_back'] == '1') { |
| 65 | + // Prevent deleting the side that is configured as the default style location |
| 66 | + if ($styleLocation === 'back') { |
| 67 | + echo json_encode(array('success' => false, 'error' => 'Cannot delete back design because default style location is back')); |
| 68 | + exit; |
| 69 | + } |
| 70 | + if ($existingBack && $existingBack !== '') { |
| 71 | + $oldPath = UPLOAD_DIR . $existingBack; |
| 72 | + if (file_exists($oldPath)) {@unlink($oldPath);} |
| 73 | + } |
| 74 | + $newBack = ''; |
| 75 | + } |
| 76 | + |
| 77 | + // handle uploaded files if provided |
| 78 | + if (isset($_FILES['frontFile']) && $_FILES['frontFile']['error'] === UPLOAD_ERR_OK) { |
| 79 | + $file = $_FILES['frontFile']; |
| 80 | + $orig = basename($file['name']); |
| 81 | + // check DB for existing use of this filename by other products |
| 82 | + $check = $conn->prepare("SELECT product_id FROM products WHERE (filename_front = ? OR filename_back = ?) AND product_id != ? LIMIT 1"); |
| 83 | + $check->bind_param("ssi", $orig, $orig, $product_id); |
| 84 | + $check->execute(); |
| 85 | + $cres = $check->get_result(); |
| 86 | + if ($cres && $cres->num_rows > 0) { |
| 87 | + echo json_encode(array('success' => false, 'error' => 'Filename "' . $orig . '" is already used by another product')); |
| 88 | + exit; |
| 89 | + } |
| 90 | + $target = UPLOAD_DIR . $orig; |
| 91 | + // delete the old product file first (per requirement) |
| 92 | + if ($existingFront && $existingFront !== '') { |
| 93 | + $oldPath = UPLOAD_DIR . $existingFront; |
| 94 | + if (file_exists($oldPath)) {@unlink($oldPath);} |
| 95 | + } |
| 96 | + // if a file already exists at the target filename, remove it so we cleanly replace |
| 97 | + if (file_exists($target)) {@unlink($target);} |
| 98 | + if (!move_uploaded_file($file['tmp_name'], $target)) { |
| 99 | + echo json_encode(array('success' => false, 'error' => 'Cannot upload front file')); |
| 100 | + exit; |
| 101 | + } |
| 102 | + $newFront = $orig; |
| 103 | + } |
| 104 | + |
| 105 | + if (isset($_FILES['backFile']) && $_FILES['backFile']['error'] === UPLOAD_ERR_OK) { |
| 106 | + $file = $_FILES['backFile']; |
| 107 | + $orig = basename($file['name']); |
| 108 | + // check DB for existing use of this filename by other products |
| 109 | + $check = $conn->prepare("SELECT product_id FROM products WHERE (filename_front = ? OR filename_back = ?) AND product_id != ? LIMIT 1"); |
| 110 | + $check->bind_param("ssi", $orig, $orig, $product_id); |
| 111 | + $check->execute(); |
| 112 | + $cres = $check->get_result(); |
| 113 | + if ($cres && $cres->num_rows > 0) { |
| 114 | + echo json_encode(array('success' => false, 'error' => 'Filename "' . $orig . '" is already used by another product')); |
| 115 | + exit; |
| 116 | + } |
| 117 | + $target = UPLOAD_DIR . $orig; |
| 118 | + // delete the old product file first (per requirement) |
| 119 | + if ($existingBack && $existingBack !== '') { |
| 120 | + $oldPath = UPLOAD_DIR . $existingBack; |
| 121 | + if (file_exists($oldPath)) {@unlink($oldPath);} |
| 122 | + } |
| 123 | + // if a file already exists at the target filename, remove it so we cleanly replace |
| 124 | + if (file_exists($target)) {@unlink($target);} |
| 125 | + if (!move_uploaded_file($file['tmp_name'], $target)) { |
| 126 | + echo json_encode(array('success' => false, 'error' => 'Cannot upload back file')); |
| 127 | + exit; |
| 128 | + } |
| 129 | + $newBack = $orig; |
| 130 | + } |
| 131 | + |
| 132 | + // Update product including possible new filenames |
30 | 133 | $query = $conn->prepare("UPDATE products |
31 | | - SET product_name = ?, price = ?, tag_list = ?, tColors = ?, lColors = ?, cColors = ?, hColors = ?, categories = ?, subcategories = ?, default_style = ?, default_style_location = ?, CustomDetailsRequired = ?, sizesAvailable = ? |
| 134 | + SET product_name = ?, price = ?, tag_list = ?, tColors = ?, lColors = ?, cColors = ?, hColors = ?, categories = ?, subcategories = ?, default_style = ?, default_style_location = ?, CustomDetailsRequired = ?, sizesAvailable = ?, filename_front = ?, filename_back = ? |
32 | 135 | WHERE product_id = ?;"); |
33 | | - $query->bind_param("ssssssssssssss", $productName, $price, $tags, $tColors, $lColors, $cColors, $hColors, $categories, $subcategories, $defaultStyle, $styleLocation, $customDetailsRequired, $sizeAvailable, $product_id); |
| 136 | + $query->bind_param("sssssssssssssssi", $productName, $price, $tags, $tColors, $lColors, $cColors, $hColors, $categories, $subcategories, $defaultStyle, $styleLocation, $customDetailsRequired, $sizeAvailable, $newFront, $newBack, $product_id); |
34 | 137 | if (!$query->execute()) { |
35 | 138 | // If insertion fails, return error message |
36 | | - echo json_encode("ERR: Insertion failed to execute" . $query->error); |
| 139 | + echo json_encode(array('success' => false, 'error' => 'DB update failed: ' . $query->error)); |
37 | 140 | } |
38 | 141 | else { |
39 | | - echo json_encode(1); |
| 142 | + echo json_encode(array('success' => true)); |
40 | 143 | } |
41 | 144 | } |
42 | 145 |
|
|
0 commit comments