我有一个看起来像这样的 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;
}
它插入到一个名为成分的数据库中。我的表格如下所示:
这是我的 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 table 中重复ingredients
,但所有后续行只插入一次。为什么它会这样做?
感谢大家的帮助!如果您需要更多详细信息,请询问!