2013-03-27 74 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; 
}