You can create form fields with array notes, for example:
<input type="text" name="quantity[productid]">
So, you can dynamically generate some fields in your form:
<input type="text" name="quantity[3]">
<input type="text" name="quantity[4]">
<input type="text" name="quantity[2]">
And then in PHP it will become an array that you can easily iterate over:
foreach ($_POST['quantity'] as $productId => $quantity) {
echo (int) $productId . ':' . (int) $quantity;
}