2013-03-27 72 views
0

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

感谢您的帮助!如果您需要更多的细节,只需询问!

+0

你检查什么的,从形式过来?例如'的var_dump($ _ POST)'。确保没有什么东西被欺骗了。然后在每个阶段回显你的查询语句,看看你是否得到任何愚蠢的输出。并且请注意,你对[SQL注入攻击](http://bobby-tables.com)是开放的。 – 2013-03-27 21:16:07

+0

@MarcB谢谢!我var_dump(ed),并没有在那里重复。我使用CodeIgniter输入类来清理输入,对吗?有更多我需要做的SQL注入? – Muhambi 2013-03-27 21:21:03

+0

@MarcB在上面的代码中的某处,它必须循​​环查询语句,因为我有一个一切正确的地方,另一个只是再次插入第一行,但我无法弄清楚它在上面的代码中的位置? – Muhambi 2013-03-27 21:27:17

回答

1

您需要将$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; 
}