|
@@ -24,6 +24,7 @@ $spec_price = isset($_POST['spec_price']) ? $_POST['spec_price'] : array();
|
|
|
$spec_moq = isset($_POST['spec_moq']) ? $_POST['spec_moq'] : array();
|
|
|
$spec_code = isset($_POST['spec_code']) ? $_POST['spec_code'] : array();
|
|
|
$spec_sort = isset($_POST['spec_sort']) ? $_POST['spec_sort'] : array();
|
|
|
+$spec_id = isset($_POST['spec_id']) ? $_POST['spec_id'] : array();
|
|
|
|
|
|
// Redirect URL
|
|
|
$redirect_url = "products.php?Keys=" . $keys . "&Page=" . $page;
|
|
@@ -81,10 +82,10 @@ if ($is_edit) {
|
|
|
WHERE id = " . (int)$id;
|
|
|
mysqli_query($conn, $sql);
|
|
|
|
|
|
- // Clear existing specifications for this product
|
|
|
- mysqli_query($conn, "DELETE FROM product_specifications WHERE product_id = " . (int)$id);
|
|
|
+ // Track which specification IDs we're keeping in this update
|
|
|
+ $kept_spec_ids = array();
|
|
|
|
|
|
- // Add new specifications
|
|
|
+ // Process specifications
|
|
|
if (is_array($spec_name) && !empty($spec_name)) {
|
|
|
foreach ($spec_name as $key => $name) {
|
|
|
if (!empty($name) && isset($spec_price[$key]) && !empty($spec_price[$key])) {
|
|
@@ -93,23 +94,56 @@ if ($is_edit) {
|
|
|
$spec_code_value = isset($spec_code[$key]) ? mysqli_real_escape_string($conn, $spec_code[$key]) : '';
|
|
|
$spec_sort_value = isset($spec_sort[$key]) && is_numeric($spec_sort[$key]) ? (int)$spec_sort[$key] : 0;
|
|
|
$spec_value_value = isset($spec_value[$key]) ? mysqli_real_escape_string($conn, $spec_value[$key]) : '';
|
|
|
+ $spec_id_value = isset($spec_id[$key]) && is_numeric($spec_id[$key]) ? (int)$spec_id[$key] : 0;
|
|
|
|
|
|
- $sql = "INSERT INTO product_specifications
|
|
|
- (product_id, spec_name, spec_value, price, min_order_quantity, spec_code, addtime, sort_order)
|
|
|
- VALUES (
|
|
|
- " . (int)$id . ",
|
|
|
- '" . mysqli_real_escape_string($conn, $name) . "',
|
|
|
- '" . $spec_value_value . "',
|
|
|
- " . $spec_price_value . ",
|
|
|
- " . $spec_moq_value . ",
|
|
|
- '" . $spec_code_value . "',
|
|
|
- NOW(),
|
|
|
- " . $spec_sort_value . "
|
|
|
- )";
|
|
|
- mysqli_query($conn, $sql);
|
|
|
+ if ($spec_id_value > 0) {
|
|
|
+ // Update existing specification
|
|
|
+ $sql = "UPDATE product_specifications SET
|
|
|
+ spec_name = '" . mysqli_real_escape_string($conn, $name) . "',
|
|
|
+ spec_value = '" . $spec_value_value . "',
|
|
|
+ price = " . $spec_price_value . ",
|
|
|
+ min_order_quantity = " . $spec_moq_value . ",
|
|
|
+ spec_code = '" . $spec_code_value . "',
|
|
|
+ sort_order = " . $spec_sort_value . "
|
|
|
+ WHERE id = " . $spec_id_value . " AND product_id = " . (int)$id;
|
|
|
+ mysqli_query($conn, $sql);
|
|
|
+
|
|
|
+ // Add to kept IDs list
|
|
|
+ $kept_spec_ids[] = $spec_id_value;
|
|
|
+ } else {
|
|
|
+ // Insert new specification
|
|
|
+ $sql = "INSERT INTO product_specifications
|
|
|
+ (product_id, spec_name, spec_value, price, min_order_quantity, spec_code, addtime, sort_order)
|
|
|
+ VALUES (
|
|
|
+ " . (int)$id . ",
|
|
|
+ '" . mysqli_real_escape_string($conn, $name) . "',
|
|
|
+ '" . $spec_value_value . "',
|
|
|
+ " . $spec_price_value . ",
|
|
|
+ " . $spec_moq_value . ",
|
|
|
+ '" . $spec_code_value . "',
|
|
|
+ NOW(),
|
|
|
+ " . $spec_sort_value . "
|
|
|
+ )";
|
|
|
+ mysqli_query($conn, $sql);
|
|
|
+
|
|
|
+ // Add newly inserted ID to kept list
|
|
|
+ $kept_spec_ids[] = mysqli_insert_id($conn);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // Delete specifications that were removed in the form
|
|
|
+ if (!empty($kept_spec_ids)) {
|
|
|
+ $delete_sql = "DELETE FROM product_specifications WHERE product_id = " . (int)$id;
|
|
|
+ if (count($kept_spec_ids) > 0) {
|
|
|
+ $delete_sql .= " AND id NOT IN (" . implode(',', $kept_spec_ids) . ")";
|
|
|
+ }
|
|
|
+ mysqli_query($conn, $delete_sql);
|
|
|
+ } else {
|
|
|
+ // If no specifications are kept, delete all specifications for this product
|
|
|
+ mysqli_query($conn, "DELETE FROM product_specifications WHERE product_id = " . (int)$id);
|
|
|
+ }
|
|
|
} else {
|
|
|
// Insert new product
|
|
|
$sql = "INSERT INTO products (ProductName, ProductImg, Addtime, moq, unit, nosale, note, tips, category_id)
|