Q: adding to database inside a foreach loop based on a specific variable in php I have a shopping cart that has a text box to update the quantity of each item. When a user clicks the update button I loop through each of the items and update their quantities in the database table holding the cart information. Now I am adding a gift card feature and I am running into a problem. When a user updates the quantity of a certain price gift card I need to not only update the carts table but also create another gift card with a unique card number. I am trying to figure out how to add another gift card if the item is a gift card while in the foreach loop. Here is the code I have for updating the quntities:
foreach ($_POST['quantity'] as $sku => $qty) {
        list($pid, $szid) = explode("-", $sku);
        if ($pid) { $pid=trim($pid);}
        if ($szid) { $szid=ltrim($szid);}
        if (isset($pid, $szid)) {
            $qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0))) ? $qty : 1;
            $r = mysqli_query($dbc, "CALL update_cart('$uid', $pid, $qty, $szid)");

        }
    }

This code works exactly as I want it to updating the quanities of the cart items. Now I cant figure out how to alter this for checking if the $pid is 1 and adding a new gift card to the gift card table if it is. Here is my best attempt but it doesnt seem to work:
foreach ($_POST['quantity'] as $sku => $qty) {
        list($pid, $szid) = explode("-", $sku);
        if ($pid) { $pid=trim($pid);}
        if ($szid) { $szid=ltrim($szid);}
        if (isset($pid, $szid)) {
            $qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0))) ? $qty : 1;
            $r = mysqli_query($dbc, "CALL update_cart('$uid', $pid, $qty, $szid)");
            while($pid == 1 ) {
                $gcn = md5(uniqid('biped', true));
                $card = 'GC'.$gcn.'';
                $amt = $_POST['price'];
                // Add to gift card table
                $r = mysqli_query($dbc, "CALL add_gift_card('$szid', '$uid', '$card', '$amt')");
            }
        }
    }

I need the code to check how many $qty there are for the gift card $pid and run the add_gift_card query for each $qty where the $pid is equal to 1. 
Oh and here is the form in case you want to see it:
echo'<input type="text" class="cart-item-quantity" name="quantity['.$cart['sku'].']" value="'.$cart['quantity'].'" size="" />';
                                            echo'<input type="hidden" name="price" value="'.$price.'" />';

Any help or guidance would be great. 

A: if ($pid == 1) {
    for ($i = 0; $i < $qty; $i++) {
        $gcn = md5(uniqid('biped', true));
        $card = 'GC'.$gcn.'';
        $amt = $_POST['price'];
        // Add to gift card table
        $stmt = mysqli_prepare($dbc, "CALL add_gift_card(?, ?, ?, ?)";
        mysqli_stmt_bind_param($stmt, 'sisi', $szid, $uid, $card, $amt);
        $r = mysqli_stmt_execute($stmt);
    }
}

