php - 插入复制第一个插入,但不复制任何其他插入

标签 php html mysql forms codeigniter

我有一个如下所示的 PHP 脚本:

foreach($_POST as $key => $value) {
      $value = $this->input->post($key);
      $ingredientQTY = $this->input->post('ingredientQTY');
      $measurements = $this->input->post('measurements');
      $ingredientNAME = $this->input->post('ingredientNAME');
      $ingredientsROW[] = array($ingredientQTY, $measurements, $ingredientNAME);
    
      for ($i = 0, $count = count($ingredientQTY); $i < $count; $i++) {
            $rows[] = array(
                'ingredientamount'         => $ingredientQTY[$i],
                'ingredientType' =>  $measurements[$i],
                'ingredientname'        => $ingredientNAME[$i],
                'recipe_id' => $recipe_id,
                'order' => $i + 1,
                'user_id' => $user_id
            );
            $sql = "INSERT `ingredients` (`ingredientamount`,`ingredientType`,`ingredientname`, `recipe_id`, `order`, `user_id`) VALUES ";
            $coma = '';
            foreach ($rows as $oneRow) {
                $sql .= $coma."('".implode("','",$oneRow)."')";
                $coma = ', ';
            }
            $this->db->query($sql);
      }
      break;
    }

它将插入到名为“成分”的数据库中。我的表单如下所示:

enter image description here

这是我的 HTML:

<span>
  <input type="text" class='pluralizer small' name="ingredientQTY[]" placeholder='QTY'/>
  <select name='measurements[]'>
    <option value='' name='' checked='checked' data-single="--" data-other="--">--</option>
    <?foreach ($measurements as $m):?>
        <option value='<?=$m->measurement;?>' data-single="<?=$m->measurement;?>" data-other="<?=$m->measurementPlural;?>">
          </option>
    <?endforeach;?>
  </select>
  <input type="text" name="ingredientNAME[]" class="ingredient" placeholder='Ingredient'/>
  <a class='float-right delete-button deleteThis' style='margin:10px 2px;' href='#'><img src='<? echo base_url()."public/img/delete.png";?>' height='11' width='11' /></a>
</span>

由于某种原因,当我插入时(工作正常,除了我要提到的问题),我插入的第一行在 mysql 表 ingredients 中重复,但所有后续行行仅插入一次。到底为什么要这么做?

感谢大家的帮助!如果您需要更多详细信息,请询问!

最佳答案

您需要将 $this->db->query($sql); 移出 for 循环,并将 $rows 重置为空数组foreach 循环的每次迭代。

试试这个:

foreach($_POST as $key => $value) {
  $value = $this->input->post($key);
  $ingredientQTY = $this->input->post('ingredientQTY');
  $measurements = $this->input->post('measurements');
  $ingredientNAME = $this->input->post('ingredientNAME');
  $ingredientsROW[] = array($ingredientQTY, $measurements, $ingredientNAME);
  $rows = array();
  for ($i = 0, $count = count($ingredientQTY); $i < $count; $i++) {
        $rows[] = array(
            'ingredientamount'         => $ingredientQTY[$i],
            'ingredientType' =>  $measurements[$i],
            'ingredientname'        => $ingredientNAME[$i],
            'recipe_id' => $recipe_id,
            'order' => $i + 1,
            'user_id' => $user_id
        );
        $sql = "INSERT `ingredients` (`ingredientamount`,`ingredientType`,`ingredientname`, `recipe_id`, `order`, `user_id`) VALUES ";
        $coma = '';
        foreach ($rows as $oneRow) {
            $sql .= $coma."('".implode("','",$oneRow)."')";
            $coma = ', ';
        }
  }
  $this->db->query($sql);
  break;
}

关于php - 插入复制第一个插入,但不复制任何其他插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15669561/

相关文章:

php - 如何开始使用PHP创建自定义支付系统?

php - 如何在php中编辑一个特定的id

jQuery 替换文本问题

html - 居中缩放溢出文本

javascript - 注册表无法正常工作并将数据存储在数据库中(PHP/MYSQL)

mysql - 使用unix按月对sql结果进行排序

php - 我无法使用 JSON 发送 POST 变量

php shell_exec($cmd) 不在 cronjob 中运行

html - 如何以设定的宽度居中 Flexbox 粘性页脚

php - Laravel Maatwebsite/Laravel-Excel 更新(如果存在)