$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, unit, moq, nosale, note, tips) VALUES ( '" . mysqli_real_escape_string($conn, $product_name) . "', '" . mysqli_real_escape_string($conn, $product_img) . "', NOW(), '" . mysqli_real_escape_string($conn, $unit) . "', '" . mysqli_real_escape_string($conn, $moq) . "', '" . $nosale_str . "', '" . mysqli_real_escape_string($conn, $note) . "', '" . mysqli_real_escape_string($conn, $tips) . "' )"; 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(); } } ?>