PHP Foreach MySQL查询

This is a receipt I'm trying to generate but i also need it to deduct quantity from our webshop. It works correctly when I only put one product through but I want it to work for each product displayed on the receipt.

<?php $r = 1; foreach ($rows as $row):?>


        <tr>

            <td style="text-align:center; width:30px;"><?php echo $r; ?></td>

            <td style="text-align:left; width:180px;"><?php echo $row->product_code; ?></td>

            <td style="text-align:center; width:50px;"><?php echo $row->quantity; ?></td>

            <td style="text-align:right; width:70px; "><?php echo $row->gross_total; ?></td>

        </tr> 

<?php

    mysqli_query($con,"UPDATE product SET quantity = quantity - '$row->quantity;' WHERE mpn = '$row->product_code'");

    mysqli_close($con);

?>

<?php $r++; endforeach; ?>

Upon adding more than 1 item to the receipt I get this error.

A PHP Error was encountered

Severity: Warning Message: mysqli_query(): Couldn't fetch mysqli Filename: views/view.php Line Number: 142

take mysqli_close($con); after endforeach;