php - 如何从表单中获取多个值并用php将它们分开

标签 php mysql arrays eloquent slim

我不知道这种形式是如何完成的。我不确定这个方法是否正确。

我创建了一个表单,用于从产品列表中创建交货。这些产品在 SQL 表中具有 ID (product_id) 和数量。

enter image description here

HTML 代码

<input type="hidden" name="quantity[]"  value="{{ product.product_id }}">
<input type="text" name="quantity[]" id="quantity" value="0" placeholder="Quantity" class="form-control form-control-sm">

我需要获取该特定产品的数量值。直到这里我才明白,但我不知道如何用 PHP 分离数量数组,使其像字符串一样,以便我可以更新数据库中的产品数量。

具有以下输入的数量输出:

enter image description here

$quantity = $request->getParam('quantity');
var_dump($quantity);
die();

enter image description here

我正在使用 slim Framework 3 和 eloquent

最佳答案

您可以使用product_id作为数量数组的键,这将为您提供一个格式良好的数组供您使用。

<input type="text" name="quantity[{{ product.product_id }}]" id="quantity" value="0">

这应该给你:

array (size=1)
  'quantity' => 
    array (size=2)
      'ABC' => string '0' (length=1)
      'DEF' => string '0' (length=1)

这样,您就可以:

foreach ($request->getParam('quantity') as $productId => $quantity) {
    // Create/update query.
}

关于php - 如何从表单中获取多个值并用php将它们分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57165086/

相关文章:

php - MYSQL LIKE 类别由于 % 返回错误查询

php - 在php中使用preg_replace删除php中的空格

php - 使用 PHP 将数据从 MySQL 导入数组和表?

php - 我来自 phpunit 的assertEquals 说我的字符串不相等,但我已经复制了它们,我认为这是由于行被添加到响应中

python - 写双(三)和作为内积?

python - 如何检查 numpy 数组是否已经存在?

c++ - 将十进制数转换为二进制数时遇到问题

mysql - 将 MySQL 身份验证插件更改为 `caching_sha2_password`

php - 在 PHP 中创建对象和数组的复杂 JSON 嵌套

php - "with"中的 Select 语句返回空数组