save_product.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require_once('conn.php');
  3. // Check login status
  4. checkLogin("信息管理");
  5. // Initialize all variables to avoid undefined warnings
  6. $id = isset($_POST['id']) ? $_POST['id'] : '';
  7. $product_name = isset($_POST['ProductName']) ? htmlspecialcharsFix($_POST['ProductName']) : '';
  8. $product_img = isset($_POST['ProductImg']) ? htmlspecialcharsFix($_POST['ProductImg']) : '';
  9. $unit = isset($_POST['unit']) ? htmlspecialcharsFix($_POST['unit']) : '';
  10. $moq = isset($_POST['moq']) ? htmlspecialcharsFix($_POST['moq']) : '';
  11. $category_id = isset($_POST['category_id']) ? intval($_POST['category_id']) : 0;
  12. $nosale = isset($_POST['nosale']) ? $_POST['nosale'] : array();
  13. $num = isset($_POST['num']) ? $_POST['num'] : array();
  14. $price = isset($_POST['price']) ? $_POST['price'] : array();
  15. $note = isset($_POST['note']) ? htmlspecialcharsFix($_POST['note']) : '';
  16. $tips = isset($_POST['tips']) ? htmlspecialcharsFix($_POST['tips']) : '';
  17. $keys = isset($_POST['keys']) ? $_POST['keys'] : '';
  18. $page = isset($_POST['page']) ? $_POST['page'] : 1;
  19. // Redirect URL
  20. $redirect_url = "products.php?Keys=" . $keys . "&Page=" . $page;
  21. if ($category_id) {
  22. $redirect_url .= "&category_id=" . $category_id;
  23. }
  24. // Determine if this is an edit or a new record
  25. $is_edit = (!empty($id) && is_numeric($id));
  26. // Process nosale array into comma-separated string
  27. $nosale_str = '';
  28. if (is_array($nosale) && !empty($nosale)) {
  29. $nosale_clean = array_map('intval', $nosale); // Ensure all values are integers
  30. $nosale_str = implode(',', $nosale_clean);
  31. }
  32. // Validate form data (add your validation here)
  33. if (empty($product_name)) {
  34. // You could add error handling here
  35. header("Location: " . $redirect_url);
  36. exit();
  37. }
  38. if ($is_edit) {
  39. // Update existing product
  40. $sql = "UPDATE products SET
  41. ProductName = '" . mysqli_real_escape_string($conn, $product_name) . "',
  42. ProductImg = '" . mysqli_real_escape_string($conn, $product_img) . "',
  43. Addtime = NOW(),
  44. moq = '" . mysqli_real_escape_string($conn, $moq) . "',
  45. unit = '" . mysqli_real_escape_string($conn, $unit) . "',
  46. nosale = '" . $nosale_str . "',
  47. note = '" . mysqli_real_escape_string($conn, $note) . "',
  48. tips = '" . mysqli_real_escape_string($conn, $tips) . "',
  49. category_id = " . $category_id . "
  50. WHERE id = " . (int)$id;
  51. mysqli_query($conn, $sql);
  52. // Handle price updates
  53. mysqli_query($conn, "DELETE FROM price WHERE productId = " . (int)$id . " AND AreaId = 0");
  54. if (is_array($num) && is_array($price)) {
  55. foreach ($num as $key => $num_value) {
  56. if (isset($price[$key])) { // Only process if we have both num and price
  57. $num_value = empty($num_value) ? 0 : (float)$num_value;
  58. $price_value = empty($price[$key]) ? 0 : (float)$price[$key];
  59. $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES
  60. (" . (int)$id . ", 0, " . $num_value . ", '" . $price_value . "')";
  61. mysqli_query($conn, $sql);
  62. }
  63. }
  64. }
  65. } else {
  66. // Insert new product
  67. $sql = "INSERT INTO products (ProductName, ProductImg, Addtime, moq, unit, nosale, note, tips, category_id)
  68. VALUES (
  69. '" . mysqli_real_escape_string($conn, $product_name) . "',
  70. '" . mysqli_real_escape_string($conn, $product_img) . "',
  71. NOW(),
  72. '" . mysqli_real_escape_string($conn, $moq) . "',
  73. '" . mysqli_real_escape_string($conn, $unit) . "',
  74. '" . $nosale_str . "',
  75. '" . mysqli_real_escape_string($conn, $note) . "',
  76. '" . mysqli_real_escape_string($conn, $tips) . "',
  77. " . $category_id . "
  78. )";
  79. mysqli_query($conn, $sql);
  80. $id = mysqli_insert_id($conn);
  81. // Handle price insertions
  82. if (is_array($num) && is_array($price)) {
  83. foreach ($num as $key => $num_value) {
  84. if (isset($price[$key])) { // Only process if we have both num and price
  85. $num_value = empty($num_value) ? 0 : (float)$num_value;
  86. $price_value = empty($price[$key]) ? 0 : (float)$price[$key];
  87. $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES
  88. (" . (int)$id . ", 0, " . $num_value . ", '" . $price_value . "')";
  89. mysqli_query($conn, $sql);
  90. }
  91. }
  92. }
  93. }
  94. // Redirect after save
  95. mysqli_close($conn);
  96. header("Location: " . $redirect_url);
  97. exit();