Php 插入复制第一次插入,但不复制任何其他插入
我有一个PHP脚本,如下所示:Php 插入复制第一次插入,但不复制任何其他插入,php,html,mysql,forms,codeigniter,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'); $ingredientN
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;
}
foreach($\u发布为$key=>$value){
$value=$this->input->post($key);
$IngreditQty=$this->input->post('IngreditQty');
$measurements=$this->input->post('measurements');
$IngreditName=$this->input->post('IngreditName');
$ingredientsROW[]=数组($ingredientQTY$MEATURES$ingredientNAME);
对于($i=0,$count=count($ingredientQTY);$i<$count;$i++){
$rows[]=数组(
“IngRedientMount”=>$ingredientQTY[$i],
'ingredientType'=>$measurements[$i],
'ingredientname'=>$ingredientname[$i],
“配方id”=>$recipe\u id,
“订单”=>$i+1,
'user\u id'=>$user\u id
);
$sql=“插入`components`(`IngRedientMount`、`ingredientType`、`ingredientname`、`recipe\u id`、`order`、`user\u id`)值”;
$coma='';
foreach($oneRow作为行){
$sql.=$coma.(“.”内爆(“,”,$oneRow)。“)”;
$coma=',';
}
$this->db->query($sql);
}
打破
}
将其插入到名为“配料”的数据库中。我的表单如下所示:
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表中被复制,但是所有后续行只插入一次。为什么会这样
谢谢你的帮助!如果你需要更多的细节,尽管问吧 您需要移动$this->db->query($sql)
在for循环之外,并在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;
}
foreach($\u发布为$key=>$value){
$value=$this->input->post($key);
$IngreditQty=$this->input->post('IngreditQty');
$measurements=$this->input->post('measurements');
$IngreditName=$this->input->post('IngreditName');
$ingredientsROW[]=数组($ingredientQTY$MEATURES$ingredientNAME);
$rows=array();
对于($i=0,$count=count($ingredientQTY);$i<$count;$i++){
$rows[]=数组(
“IngRedientMount”=>$ingredientQTY[$i],
'ingredientType'=>$measurements[$i],
'ingredientname'=>$ingredientname[$i],
“配方id”=>$recipe\u id,
“订单”=>$i+1,
'user\u id'=>$user\u id
);
$sql=“插入`components`(`IngRedientMount`、`ingredientType`、`ingredientname`、`recipe\u id`、`order`、`user\u id`)值”;
$coma='';
foreach($oneRow作为行){
$sql.=$coma.(“.”内爆(“,”,$oneRow)。“)”;
$coma=',';
}
}
$this->db->query($sql);
打破
}
您是否检查了表单中的内容?e、 g.var\u dump($\u POST)
。确保那里没有人上当受骗。然后在每个阶段回显您的查询语句,查看是否得到任何重复输出。请注意,您对这项服务非常开放。@MarcB谢谢!I var_dump(ed)并且它没有在那里复制。我使用的是CodeIgniter输入类,它对输入进行净化,对吗?。SQL注入还有什么需要做的吗?@MarcB在上面代码的某个地方,它一定是在循环查询语句,因为我有一个查询语句,所有内容都是正确的,另一个只是再次插入第一行,但我不知道它在上面的代码中是在哪里做的?不知道。虽然您正在执行的foreach(…=>$value){$value=…
对我来说似乎有点可疑。如果您只需要数组键,您可以改为执行foreach(数组键($\u POST)作为$key)
。