alert('错误:国家名称不能为空!');history.back();";
exit;
}
if (trim($countryCode) === '') {
echo "";
exit;
}
// 规格相关数据
$specProductIds = $_POST['spec_product_id'] ?? [];
$specIds = $_POST['spec_id'] ?? [];
$specPrices = $_POST['spec_price'] ?? [];
$specMoqs = $_POST['spec_moq'] ?? [];
// 验证售价不能为0
$hasZeroPrice = false;
$zeroProductName = '';
$zeroSpecName = '';
if (!empty($specProductIds) && !empty($specIds) && is_array($specProductIds) && is_array($specIds)) {
for ($i = 0; $i < count($specProductIds); $i++) {
if (isset($specPrices[$i])) {
$price = trim($specPrices[$i]);
if ($price === '' || $price === '0' || floatval($price) <= 0) {
// 查询产品和规格名称
$productId = intval($specProductIds[$i]);
$specId = intval($specIds[$i]);
$nameQuery = "SELECT p.ProductName, ps.spec_name
FROM products p
JOIN product_specifications ps ON p.id = ps.product_id
WHERE p.id = $productId AND ps.id = $specId";
$nameResult = $conn->query($nameQuery);
if ($nameResult && $nameRow = $nameResult->fetch_assoc()) {
$zeroProductName = $nameRow['ProductName'];
$zeroSpecName = $nameRow['spec_name'];
}
$hasZeroPrice = true;
break;
}
}
}
}
if ($hasZeroPrice) {
echo "";
exit;
}
if ($isedit) {
$sql = "UPDATE country SET
countryName = '" . $conn->real_escape_string($countryName) . "',
countryCode = '" . $conn->real_escape_string($countryCode) . "',
addtime = NOW()
WHERE id = $id";
$conn->query($sql);
// 删除该国家的所有产品规格价格
$conn->query("DELETE FROM product_country_price WHERE country_id=$id");
// 处理规格价格数据
if (!empty($specProductIds) && !empty($specIds) && is_array($specProductIds) && is_array($specIds)) {
for ($i = 0; $i < count($specProductIds); $i++) {
if (isset($specPrices[$i]) && isset($specMoqs[$i]) && isset($specIds[$i])) {
$productId = intval($specProductIds[$i]);
$specId = intval($specIds[$i]);
$specPrice = $conn->real_escape_string($specPrices[$i]);
$specMoq = empty($specMoqs[$i]) ? 1 : intval($specMoqs[$i]);
if ($productId > 0 && $specId > 0) {
$sql = "INSERT INTO product_country_price (country_id, product_id, specification_id, price, min_order_quantity, addtime)
VALUES ($id, $productId, $specId, '$specPrice', $specMoq, NOW())";
$conn->query($sql);
}
}
}
}
$page = $_GET['Page'] ?? '';
$keys = urlencode($_GET['Keys'] ?? '');
$ord = urlencode($_GET['Ord'] ?? '');
header("Location: ?keys=$keys&Ord=$ord&Page=$page");
exit;
} else {
// For new country, include countryName and countryCode
$sql = "INSERT INTO country (countryName, countryCode, addtime) VALUES (
'" . $conn->real_escape_string($countryName) . "',
'" . $conn->real_escape_string($countryCode) . "',
NOW()
)";
$conn->query($sql);
$id = $conn->insert_id;
// 处理规格价格数据
if (!empty($specProductIds) && !empty($specIds) && is_array($specProductIds) && is_array($specIds)) {
for ($i = 0; $i < count($specProductIds); $i++) {
if (isset($specPrices[$i]) && isset($specMoqs[$i]) && isset($specIds[$i])) {
$productId = intval($specProductIds[$i]);
$specId = intval($specIds[$i]);
$specPrice = $conn->real_escape_string($specPrices[$i]);
$specMoq = empty($specMoqs[$i]) ? 1 : intval($specMoqs[$i]);
if ($productId > 0 && $specId > 0) {
$sql = "INSERT INTO product_country_price (country_id, product_id, specification_id, price, min_order_quantity, addtime)
VALUES ($id, $productId, $specId, '$specPrice', $specMoq, NOW())";
$conn->query($sql);
}
}
}
}
header("Location: ?");
exit;
}
}
if ($act == "add" || $act == "edit") {
$id = $_GET['id'] ?? '';
$isedit = false;
$countryCode = '';
$countryName = '';
$addtime = '';
if ($id != "" && is_numeric($id)) {
$isedit = true;
$sql = "SELECT * FROM country WHERE id = $id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row) {
$countryCode = $row['countryCode'];
$countryName = $row['countryName'];
$addtime = $row['addtime'];
} else {
$isedit = false;
}
}
$page = $_GET['Page'] ?? '';
$keys = urlencode($_GET['Keys'] ?? '');
$ord = urlencode($_GET['Ord'] ?? '');
$hrefstr = "?keys=$keys&Ord=$ord&Page=$page";
?>