嵌套的foreach循环破坏了我的一天。数据插入多次(根据检查的嵌套菜单的数量插入相同的值),并且嵌套值存储在每一行中,尽管它们不是父级。
我认为问题是由嵌套的foreach循环引起的
<?php
if(!empty($_POST['heading']) && !empty($_POST['content']) && !empty($_POST['keytag']) && !empty($_POST['date'])){
$heading=$_POST['heading'];
$content=$_POST['content'];
$keytag=$_POST['keytag'];
$date=$_POST['date'];
$query = '';
$msg = '';
$data = explode("," ,$_POST["navid"]);
$subdata = explode("," ,$_POST["subnavid"]);
foreach ($data AS $key => $menu){
foreach($subdata AS $k => $submenu){
//If Image is browsed
if(!empty($_FILES['file']['name'])){
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
$query = $con->prepare('INSERT into news(heading, cat_id, subcat_id, content, keytag, date, img) VALUES(:heading, :cat_id, :subcat_id, :content, :keytag, :date, :new_img)');
$query->bindParam(':new_img',$new_img);
}
//If Image isnot browsed
if(empty($_FILES['file']['name'])){
$query = $con->prepare('INSERT into news(heading, cat_id, subcat_id, content, keytag, date) VALUES(:heading, :cat_id, :subcat_id, :content, :keytag, :date)');
}
if(empty($submenu)){
$submenu = null;
}
$query->bindParam(':heading', $heading);
$query->bindParam(':subcat_id', $submenu);
$query->bindParam(':cat_id', $menu);
$query->bindParam(':content', $content);
$query->bindParam(':keytag', $keytag);
$query->bindParam(':date', $date);
if($query->execute()){
$msg="Successfully Inserted";
};
}
}
echo $msg;
}
} ?&GT;
这是问题
让我简单解释一下。网站有三个菜单:News
,Views
,Art
。其中仅新闻菜单有子菜单。如果新闻“submenu
和Views/Art
一次 ,则默认情况下Views/Art
列也会存储submenu
值,但会显示在真实视图中/ Art没有子菜单,存储新闻子菜单值。
应该是
<li>
<input type="checkbox" name="menu[]" value="2">News
<ul>
<li>
<input type="checkbox" name="menu[][menusub]" value="2">Politics
</li>
<li>
<input type="checkbox" name="menu[][menusub]" value="3">Social
</li>
<li>
<input type="checkbox" name="menu[][menusub]" value="4">Economy
</li>
</ul>
</li>
<li>
<input type="checkbox" name="menu[]" value="12">Views
</li>
<li>
<input type="checkbox" name="menu[]" value="13">ART
</li>
数据通过ajax传递,如下所示
$(document).on('submit', '#form', function(e){
e.preventDefault();
var navid = [];
$("input[name='menu[]']:checked").each(function(){
navid.push(this.value);
});
var subnavid = [];
$("input[name='menu[][menusub][]']:checked").each(function(){
subnavid.push(this.value);
});
if (navid.length === 0){ //tell you if the array is empty
alert("Please Select atleast one checkbox");
return false;
}
else {
var formData = new FormData(this);
formData.append('navid', navid);
formData.append('subnavid', subnavid);
$.ajax({
type: 'POST',
url: 'upload.php',
data: formData,
contentType: false,
cache: false,
processData:false,
success: function(data){
alert(data);
}
});
}
});
答案 0 :(得分:0)
从第一个$subdata = explode("," ,$_POST["subnavid"]);
循环中取出foreach
,你应该好好去。
因为每个父循环都会提取$_POST["subnavid"]
,子循环将从数组的第一个成员开始。
$subdata = explode("," ,$_POST["subnavid"]); // This is the change required.
foreach ($data AS $key => $menu){
foreach($subdata AS $k => $submenu){
......
......
}
}
同时更改$key
变量。因为它一样。所以它可能会与每个循环重叠。