Php 维度数组项未插入到同一DB行中
我从以下几点开始:Php 维度数组项未插入到同一DB行中,php,multidimensional-array,Php,Multidimensional Array,我从以下几点开始: <form action="" method="post" enctype="multipart/form-data" id="form"> <?php foreach($attributes as $attribute){ ?> <input type="checkbox" name="attribute[][attr_id]" value="<?php echo $attribute['attribute_id']; ?>"
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php foreach($attributes as $attribute){ ?>
<input type="checkbox" name="attribute[][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
<?php } ?>
</form>
if(isset($_POST['attribute'])){
foreach($_POST['attribute'] as $attribute){
$attr_id = $attribute['attr_id'];
$attr_name = $attribute['attr_name'];
"INSERT INTO " . DB_PREFIX . "attribute_xref SET productid = '" . $productid . "', attribute_id='". $attr_id ."', attribute_name='" . $attr_name . "'";
}
}
但是,结果与我预期的略有不同。每次选中一个框并键入其文本输入时,它们的值都会发送到两个不同的DB行:
productid -- attribute_id -- attribute_name
10 -- 102 -- empty
10 -- 0 -- somename
在上面的第二行中,属性_id的值为零,表示未被检查
我不能完全了解我的错误在哪里。最后。棘手的答案是在关联输入中添加相同的数字索引,如下所示:
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php foreach($attributes as $attribute){ ?>
<input type="checkbox" name="attribute[i][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[i][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
<?php } ?>
</form>
关于主要问题不是100%,我想php foreach循环会单独拾取每个输入,但您应该真正研究参数化查询。像那样将SQL连接在一起是一个可怕的安全风险-请查看“SQL注入”以了解更多信息。该表单仅由管理面板上的团队用户使用,这就是为什么我有一个非参数化查询,但当然这不是一种proffesonist方式。是的,我看到两个输入具有不同的数组键。因此,我想我使用了错误的方法来获得期望的结果。。。
<input type="checkbox" name="attribute[<?php echo $attribute['attribute_id']; ?>][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[<?php echo $attribute['attribute_id']; ?>][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">