2016-07-05 23 views
0

我正在測試textarea框及其對應的foreach代碼。成功工作。基本上,代碼將每個值放入textarea框中,並將每個值插入到數據庫中。對於不同變量的多個foreach?在數據庫中複製的值

但我想複製這個,因爲我有多個textarea框。

任何人都可以驗證以下代碼嗎?

$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(); 
} 

這是他們看起來像在數據庫:

DoctorateDegrees似乎工作正常,但其自身複製。

如果我把

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

到textarea的箱子,他們將每個人都有自己的行,但它會不斷重複這樣的:

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

等欄目,例如,BachelorsDegrees(這在我插入額外的foreach代碼之前一直工作正常),只會重複第一個值:

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

MastersDegrees將重複相同的值一定的時間:

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

任何想法?

注意:BachelorsDegrees之前工作之前,我在測試它之前添加在其他列。

+0

很難回答這個問題。你能提供測試輸入數據嗎?解釋你想要什麼與你在數據庫中得到什麼?內循環將爲外循環的每次迭代重複其完整範圍的值。即使每個循環數組中只有3個值和7個循環級別,您也會得到每個$ textAr值3 * 3 * 3 * 3 * 3 * 3次。那麼你肯定** BachelorsDegrees「只是重複第一個值」? –

+0

是的。它只適用於BachelorsDegrees自身,這意味着其他foreach循環和數組不在代碼中。當我把代碼插入時,第一個值會被多次插入到數據庫中。我在多行中看到相同的值。 – ThomasTheNoobCoder

+0

的確,我的觀點也一樣。假設你有兩個級別的foreach()。然後,外層將取第一個值,然後**重用**內層foreach中的每個**值。當內部foreach已經遍歷所有的值時,它回到外部和第二個值。然後內部的foreach會再次重複所有的值,現在使用來自外部循環的第二個值。所以我的直覺是你可能想要重新設計你的代碼,這可能是你想要的。 –

回答

0

我強烈建議你將College_Courses表分成幾個小表。可能每列一列(如上所述)。除非我錯過了一些東西。數據庫設計中的經驗法則是不要像這樣在同一個表中捆綁許多不同的屬性。

如果你仍然想堅持一個表的方法,你應該分開你的foreach循環,並在每個循環中執行INSERT查詢,而不是像他們現在一樣嵌套它們。不過,你最終會得到一張巨大的桌子,裏面有很多未使用的空間。

相關問題