$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 mysqli_close($conn); header("Location: " . $redirect_url); exit();