$num_value) {
if (isset($price[$key])) { // Only process if we have both num and price
$num_value = empty($num_value) ? 0 : (float)$num_value;
$price_value = empty($price[$key]) ? 0 : (float)$price[$key];
$sql = "INSERT INTO price (productId, AreaId, num, price) VALUES
(" . (int)$id . ", 0, " . $num_value . ", '" . $price_value . "')";
mysqli_query($conn, $sql);
}
}
}
} else {
// Insert new product
$sql = "INSERT INTO products (ProductName, ProductImg, Addtime, moq, unit, nosale, note, tips, category_id)
VALUES (
'" . mysqli_real_escape_string($conn, $product_name) . "',
'" . mysqli_real_escape_string($conn, $product_img) . "',
NOW(),
'" . mysqli_real_escape_string($conn, $moq) . "',
'" . mysqli_real_escape_string($conn, $unit) . "',
'" . $nosale_str . "',
'" . mysqli_real_escape_string($conn, $note) . "',
'" . mysqli_real_escape_string($conn, $tips) . "',
" . $category_id . "
)";
mysqli_query($conn, $sql);
$id = mysqli_insert_id($conn);
// Handle price insertions
if (is_array($num) && is_array($price)) {
foreach ($num as $key => $num_value) {
if (isset($price[$key])) { // Only process if we have both num and price
$num_value = empty($num_value) ? 0 : (float)$num_value;
$price_value = empty($price[$key]) ? 0 : (float)$price[$key];
$sql = "INSERT INTO price (productId, AreaId, num, price) VALUES
(" . (int)$id . ", 0, " . $num_value . ", '" . $price_value . "')";
mysqli_query($conn, $sql);
}
}
}
}
// Redirect after save
header("Location: ?keys=" . $keys . "&Page=" . $page);
exit();
}
// Handle bulk actions
if ($act == 'postchk') {
if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
$chk_ids = array_map('intval', $_POST['chkbox']);
$chk_act = (int)$_POST['chkact'];
if (!empty($chk_ids)) {
$ids_str = implode(',', $chk_ids);
switch ($chk_act) {
case 0:
case 1:
$sql = "UPDATE customer SET cs_state = " . $chk_act . " WHERE id IN (" . $ids_str . ")";
break;
case -1:
$sql = "DELETE FROM products WHERE id IN (" . $ids_str . ")";
break;
}
if (isset($sql)) {
mysqli_query($conn, $sql);
}
}
header("Location: ?Keys=" . $keys . "&Page=" . $page);
exit();
}
}
?>
产品信息管理
0);
if ($is_edit) {
$sql = "SELECT * FROM products WHERE id = " . (int)$id;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$product_name = htmlspecialcharsFix($row['ProductName']);
$product_img = htmlspecialcharsFix($row['ProductImg']);
$unit = htmlspecialcharsFix($row['unit']);
$moq = htmlspecialcharsFix($row['moq']);
$nosale = $row['nosale'];
$note = htmlspecialcharsFix($row['note']);
$tips = htmlspecialcharsFix($row['tips']);
$category_id = $row['category_id'] ? intval($row['category_id']) : 0;
} else {
// Product not found, redirect
header("Location: ?Keys=" . $keys . "&Page=" . $page);
exit();
}
}
$href_str = "?keys=" . $keys . "&Page=" . $page;
?>
0) {
// Get all subcategories of the selected category
$category_data = buildCategoryTree($conn);
$all_categories = $category_data['all_categories'];
$category_ids = array($filter_category_id);
getSubcategoryIds($all_categories, $filter_category_id, $category_ids);
$category_condition = " WHERE category_id IN (" . implode(',', $category_ids) . ")";
}
// Search condition
$search_condition = '';
if (!empty($keyscode)) {
$search_condition = ($category_condition ? " AND " : " WHERE ") .
"ProductName LIKE '%" . mysqli_real_escape_string($conn, $keyscode) . "%'";
}
// Get total records count using a COUNT query instead of fetching all records
$count_sql = "SELECT COUNT(*) as total FROM products" . $category_condition . $search_condition;
$count_result = mysqli_query($conn, $count_sql);
$count_row = mysqli_fetch_assoc($count_result);
$total_records = $count_row['total'];
// 固定每页显示18条记录(与 customers.php 保持一致)
$pageSize = 18;
$total_pages = ceil($total_records / $pageSize);
if ($total_pages < 1) $total_pages = 1; // 确保至少有一页,即使没有结果
// Validate page number
if (empty($page)) $page = 1;
if ($page == 'end') $page = $total_pages;
if (!is_numeric($page) || $page < 1) $page = 1;
$page = (int)$page;
if ($page > $total_pages) $page = $total_pages;
// Apply pagination
$offset = ($page - 1) * $pageSize;
if ($offset < 0) $offset = 0; // 确保偏移量不为负数
// Fetch only the records for the current page
$sql = "SELECT id, ProductName, ProductImg, category_id FROM products" .
$category_condition . $search_condition . " ORDER BY id DESC LIMIT $offset, $pageSize";
$result = mysqli_query($conn, $sql);
$temp_num = $pageSize * ($page - 1);
?>