多个foreach用于不同的变量?值在数据库中重复

时间:2016-07-05 22:10:21

标签: php foreach

我正在测试RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ 框及其相应的textarea代码。工作顺利。基本上,代码将每个值放在textarea框中,并将每个值插入到数据库的各个行中。

但我想复制这个,因为我有多个textarea框。

任何人都可以验证以下代码吗?

foreach

这就是他们在数据库中的样子:

DoctorateDegrees似乎工作正常,但它自身重复。

如果我把

$text = trim($_POST['BachelorsDegrees']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
$textCert = trim($_POST['Certifications']);
$textArCert = explode("\n", $textCert);
$textArCert = array_filter($textArCert, 'trim'); // remove any extra \r characters left behind
$textDip = trim($_POST['Diplomas']);
$textArDip = explode("\n", $textDip);
$textArDip = array_filter($textArDip, 'trim'); // remove any extra \r characters left behind
$textAD = trim($_POST['AssociateDegrees']);
$textArAD = explode("\n", $textAD);
$textArAD = array_filter($textArAD, 'trim'); // remove any extra \r characters left behind
$textMD = trim($_POST['MastersDegrees']);
$textArMD = explode("\n", $textMD);
$textArMD = array_filter($textArMD, 'trim'); // remove any extra \r characters left behind
$textDD = trim($_POST['DoctorateDegrees']);
$textArDD = explode("\n", $textDD);
$textArDD = array_filter($textArDD, 'trim'); // remove any extra \r characters left behind
$textSD = trim($_POST['SpecialDegreePrograms']);
$textArSD = explode("\n", $textSD);
$textArSD = array_filter($textArSD, 'trim'); // remove any extra \r characters left behind


foreach ($textAr as $BachelorsDegrees)
{
    foreach ($textArCert as $Certifications)
    {
    foreach ($textArDip as $Diplomas)
    {
    foreach ($textArAD as $AssociateDegrees)
    {
    foreach ($textArMD as $MastersDegrees)
    {
    foreach ($textArDD as $DoctorateDegrees)
    {
    foreach ($textArSD as $SpecialDegreePrograms)
    {
    mysql_query("INSERT INTO College_Courses (Name, BachelorsDegrees, Certifications,Diplomas,AssociateDegrees,MastersDegrees,DoctorateDegrees,SpecialDegreePrograms) VALUES ('$Name','$BachelorsDegrees', '$Certifications', '$Diplomas', '$AssociateDegrees', '$MastersDegrees', '$DoctorateDegrees','$SpecialDegreePrograms')") or die(mysql_error()) ;
    }
    }
    }
    }
    }
    }

} 
}
else
{
echo mysql_error();
}

进入textarea框,他们每个都有自己的行,但它会不断重复:

<p>Onion1<br>
Onion2<br>
Onion3</p>

其他列,例如,BachelorsDegrees(在我插入额外的foreach代码之前一直工作正常),只会重复第一个值:

<p>Onion1<br>
Onion2<br>
Onion3</p>
<p>Onion1<br>
Onion2<br>
Onion3</p>
<p>Onion1<br>
Onion2<br>
Onion3</p>

MastersDegrees将重复相同的值一段时间:

<p>Onion1<br>
<p>Onion1<br>
<p>Onion1<br>

有什么想法吗?

注意:<p>Onion1<br> <p>Onion1<br> <p>Onion1<br> Onion2<br> Onion2<br> Onion2<br> Onion3<br> Onion3<br> Onion3<br> 在我测试之前正在工作,然后再添加其他列。

1 个答案:

答案 0 :(得分:0)

我强烈建议您将College_Courses表拆分为几个较小的表。可能每列一个(如上所述)。除非我错过了什么。数据库设计的一个经验法则是不要在同一个表中捆绑许多不同的属性。

如果你仍然想要坚持一个表的方法,你应该分开你的foreach循环并在每个循环中执行INSERT查询,而不是像现在一样嵌套它们。不过,你最终会得到一张巨大的桌子,里面有很多未使用的空间。