当我使用while循环时,如何获得每个输入文本的单个值


How can I get the individual value of each input text when I am using a while loop?

我正在尝试创建一个表,它将在其中提供项目列表,然后用户可以勾选他们想要的项目,输入他们想要的项数。

<table name="item_orders">
<thead>
    <tr>
        <th></th>
        <th>Item</th>
        <th>Price</th>
        <th>Quantity</th>
    </tr>
</thead>
<?php   
    $list=getItemList($connect);
    while($row=mysqli_fetch_assoc($list)){
?>
    <tr>
        <td><input type="checkbox"  name="check_list[]" value="<?=$row['id']?>"/></td>
        <td><?=$row['item']?></td>
        <td><?=$row['price']?></td>
        <td><input type="text" placeholder="How Many" name="howmany"/></td>
        </tr>   
<?php   
    }
?>
</table>

然后我会用这个代码保存它

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
    $listy = getList($connect, $check) -> fetch_assoc();
    $totalamount = $listy['price'] * $howmany;
    addBalanceDB($connect, $userID, $listy['item'], $totalamount, null);
    }
}

问题是,如果用户选择2个项目,如(狗粮$200)和(猫粮$250),然后为狗粮放入5个,为猫粮放入10个。它只会得到10的值。制作狗粮2000美元,而不是1000美元

您只需要在名称后面添加括号,使其成为一个数组:

<input type="text" placeholder="How Many" name="howmany[]"/>

还要确保添加一个隐藏值来识别给定索引中的当前项目:

<input type="hidden" value="<?=$row['id']?>" name="whatis[]"/>

然后将结果放入$_POST['howmany']$_POST['whatis']

作为一种选择,你当然可以制作固定的索引或单独的名称。