gpt4 book ai didi

php - 如何将动态生成的表单不同字段值存储回数据库php mysql?

转载 作者:行者123 更新时间:2023-11-29 12:03:54 27 4
gpt4 key购买 nike

我是 php mysql 的初学者,我在下面的图片类型表单中遇到问题如何保存所有行的值或仅保存选定的行字段值:-字段名称如下:-

  foreach($data as $row){
<tr>
<td><input type="checkbox" value="$row['ProductID']" name="productID[]" /></td>
<td> <input type="text" value="$row['Quantity']" name="quantity[]" /></td>
<td><input type="text" value="$row['Price']" name="price[]" /></td>
</tr>
}

enter image description here

<强>1。问题是如何在 php 或 jquery 中仅获取选定的行字段值我不知道如何在 php 中仅获取选定的行字段值

最佳答案

如果您想获取所有项目的列表,并附有注释,无论是否选中,只需稍微更改一下表单名称(如果我明白您在寻找什么......):

<?php 
$data[] = array('ProductID'=>123,"Quantity"=>1,"Price"=>"2.00");
$data[] = array('ProductID'=>234,"Quantity"=>2,"Price"=>"1.50");
$data[] = array('ProductID'=>345,"Quantity"=>1,"Price"=>"4.59");
$data[] = array('ProductID'=>456,"Quantity"=>4,"Price"=>"1.99");

foreach($data as $row){ ?>
<tr>
<td><input type="checkbox" name="product[<?php echo $row['ProductID'];?>][select]" /></td>
<td><input type="text" value="<?php echo $row['Quantity']; ?>" name="product[<?php echo $row['ProductID'];?>][qty]" /></td>
<td><input type="text" value="<?php echo $row['Price']; ?>" name="product[<?php echo $row['ProductID'];?>][price]" /></td>
</tr>
<?php }

给你:

// Just loop through the [product] array looking for the 'select' = 'on' 
Array
(
[product] => Array
(
[123] => Array
(
[qty] => 1
[price] => 2.00
)

[234] => Array
(
[select] => on
[qty] => 2
[price] => 1.50
)

[345] => Array
(
[qty] => 1
[price] => 4.59
)

[456] => Array
(
[qty] => 4
[price] => 1.99
)
)
)

获取所选项目:

if(!empty($_POST['product'])) {
foreach($_POST['product'] as $row) {
if(!empty($row['select'])) {
print_r($row);
}
}
}

关于php - 如何将动态生成的表单不同字段值存储回数据库php mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31900366/

27 4 0