插入重复的第一个插入,但不包含任何其他插入

时间:2013-03-27 21:10:05

标签: 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;
    }

插入名为ingredients的数据库中。我的表格如下:

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中重复,但所有后续行都只是插入一次。为什么这样做呢?

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

1 个答案:

答案 0 :(得分:1)

你需要在for循环之外移动$this->db->query($sql);并在foreach循环的每次迭代中将$rows重置为空数组。

试试这个:

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