$ctr = 0;
foreach($_POST['field_name'] as $key => $field_name)
foreach($_POST['field_name1'] as $key => $field_name1)
{
{
if($_POST['field_name'][$key]){
if($_POST['field_name1'][$key]){
$sqli = "INSERT INTO farmersform (IDTwo,IDThree,IDFour,FCrops,FLandArea) VALUES ('$IDTwo','$IDThree','$IDFour','$field_name','$field_name1')";
$result = $conn->query($sqli);
if($result){
$ctr++;
}
}
}
} }
这是我的代码,如果它保存,它会在数据中保存2个类似的数据,我该如何解决这个问题?
答案 0 :(得分:2)
删除其中一个foreach
循环。假设field_name
和field_name1
具有相同的数组长度,您的代码仍然可以进行一些调整。
$ctr = 0;
foreach($_POST['field_name1'] as $key => $field_name1) {
if ($_POST['field_name'][$key]) {
if ($_POST['field_name1'][$key]) {
$sqli = "INSERT INTO farmersform (IDTwo,IDThree,IDFour,FCrops,FLandArea) VALUES ('$IDTwo','$IDThree','$IDFour','{$_POST['field_name'][$key]}','$field_name1')";
$result = $conn->query($sqli);
if ($result) {
$ctr++;
}
}
}
}