维度数组项未插入同一数据库行

Dimensional array items are not inserting in the same DB row

我从以下开始:

<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>

因此每个 $attribute 都有一个复选框和一个文本输入框;每当有人检查(一个或多个框)并插入文本(仅对选中的项目)时,我想在数据库中获取特定项目的 [attr_id][attr_name]

所以我继续以下内容:

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 . "'";
  }
 }

但是,结果和我预想的有点不同。每次选中一个框并键入其文本输入时,它们的值将发送到两个不同的数据库行:

productid   --   attribute_id   --   attribute_name
10          --       102        --        empty
10          --        0         --       somename

在上面的第二行中,attribute_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>

在我的例子中 "i" 将从 'attribute_id':

中获取变量号
<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']; ?>">

希望我的回答对以后的人也有帮助。