You can peform selects during inserts.
INSERT INTO lineitem(product_id, cost, image, qty)
SELECT product.id, product.cost, product.image, 4 FROM product WHERE product.id = 6
UNION SELECT product.id, product.cost, product.image, 10 FROM product WHERE product.id = 1
Where 4 and 10 are quantities as supplied by your user. The above will insert two items into your lineitems table, provided that product.id is unique, which it should be.
You can of course also perform one select to get all relevant products from the product table as you started out doing, and then use that data for an insert on the form INSERT INTO table VALUES ...
But, if you do it this way, there is no reason to have a specific order in the result set. Wether you insert lineitem for product 1 first then 6, or first 6 then 1 doesn't matter. And if you have your user supplied data organized in a reasonable way, then there is no extra work to deal with any kind of ordering.
// product id as array key, element holds the quantity
$product[6] = 4;
$product[1] = 10;
$query = 'SELECT id, cost, image FROM product WHERE id IN (' . implode(',', $product) . ')';
//...
if ($result) {
$values = array();
while($row = ...) {
// Reference the product quantity by its id.
$values[] = '(' . implode(',', $row) . ', ' . $product[$row['id']] . ')';
}
$update = 'INSERT INTO lineitem(product_id, cost, image, qty) VALUES ' . implode(', ', $values);
}
But to get some kind of ordering of a result set, you need to tell your dbms what to order by. '6,1' is nonsensical for this purpose. Perhaps what you were looking for would be ORDER BY id DESC.