2012-06-25 54 views
0

嘿,我該如何將數組保存到數據庫中的多行?我正在使用MySQL數據庫與PHP。將數組保存爲多行

// GETTING ALL THE B NODE STUFFS AND PRINTING IT'S CONTENTS 
    $result = array(); 
    foreach($document->getElementsByTagName('b') as $node){ 
    $result[preg_replace('/:\s+$/','',$node->textContent)] = trim($node->nextSibling->textContent); 

    } 
    var_dump($result); 

下面是結果從我的陣列中拔出。

array 
'Choose by Subject Category or Module Code' => string '' (length=0) 
' 
Back to Home page' => string '' (length=0) 
'International' => string 'visiting students should consult the' (length=36) 
'Undergraduate' => string 'students should refer to the relevant section of the UCC' (length=56) 
'Postgraduate' => string 'students should refer to the relevant section of the UCC' (length=56) 
'Credit Weighting' => string '5' (length=1) 
'Teaching Period(s)' => string 'Teaching Period 1.' (length=18) 
'No. of Students' => string 'Min 15, Max 30.' (length=15) 
'Pre-requisite(s)' => string 'None' (length=4) 
'Co-requisite(s)' => string 'None' (length=4) 
'Teaching Methods' => string '1 x 4hr(s) Lectures; Other (Distance Education Module - Up to 146hrs Self Directed Study).' (length=90) 
'Module Co-ordinator' => string 'Dr Peter Cleary, Department of Accounting, Finance and Information Systems.' (length=75) 
'Lecturer(s)' => string 'Staff, Department of Accounting, Finance and Information Systems.' (length=65) 
'Module Objective' => string 'To examine the management uses of accounting information and to enhance students ability to exert effective managerial control.' (length=127) 
'Module Content' => string 'Topics include; the accounting information needs of management, costs and pricing; estimating costs; the identification of key performance indicators; budgeting for control; capital investment appraisal and implications for strategic planning and control.' (length=256) 
'Learning Outcomes' => string 'On successful completion of this module, students should be able to:' (length=68) 
'Assessment' => string 'Total Marks 100: Continuous Assessment 100 marks (Project/ Essay. Approximately 1500 words.).' (length=93) 
'Compulsory Elements' => string 'Continuous Assessment.' (length=22) 
'Penalties (for late submission of Course/Project Work etc.)' => string 'Where work is submitted up to and including 7 days late, 10% of the total marks available shall be deducted from the mark achieved. Where work is submitted up to and including 14 days late, 20% of the total marks available shall be deducted from the mark achieved. Work submitted 15 days late or more shall be assigned a mark of zero.' (length=336) 
'Pass Standard and any Special Requirements for Passing Module' => string '40%.' (length=4) 
'End of Year Written Examination Profile' => string 'No End of Year Written Examination.' (length=35) 
'Requirements for Supplemental Examination' => string 'Marks in passed element(s) of Continuous Assessment are carried forward, Failed element(s) of Continuous Assessment must be repeated (Resubmission of revised Continuous Assessment).' (length=181) 

我也嘗試與NSERT查詢每條語句裏,但所有的內容保存在一個科拉姆來代替。

$array_data = implode("array_separator", $result); 
    foreach($result as $snode){ 
    $query = sprintf("INSERT INTO save_array (ModuleCode) VALUES ('%s')",mysql_real_escape_string($snode)); 
    mysql_query($query) or die('Error, insert query failed'); 
    echo $snode.<br />'; 
    } 
    echo '<br /><br /><br />'; 

任何幫助,將不勝感激。謝謝。

//============== LATEST INSERT QUERY================//  
$array_data = implode("array_separator", $result); 
foreach($result as $snode){ 
$query = sprintf("INSERT INTO save_array 
     (ModuleCode, 
     Homepage, 
     International, 
     Undergraduate, 
     Postgraduate, 
     CreditWeighting, 
     TeachingPeriod, 
     NoofStudents, 
     Prerequisite, 
     Corequisite, 
     TeachingMethods, 
     ModuleCoordinator, 
     Lecturer, 
     ModuleObjective, 
     ModuleContent, 
     LearningOutcomes, 
     Assessment, 
     CompulsoryElements, 
     Penalties, 
     PassStandard, 
     EndofYearWrittenExamination, 
     RequirementsforExamination) VALUES ('%s')",mysql_real_escape_string($snode)); 


foreach ($result as $key => $value) 
$query = $query . "$value"; 

echo '<br /><br />'; 
mysql_query($query) or die($query."<br/><br/>".mysql_error()); 
echo $snode. '<br />'; 
} 
echo '<br /><br /><br />'; 
+0

旁註:嘗試使用[mysqli](http://tr.php.net/manual/en/intro.mysqli.php)函數(或PDO)而不是舊的[mysql](http://tr.php .net/manual/en/intro.mysql.php),它們很快就會被移除。 – Wh1T3h4Ck5

回答

0

一個快速的回答是,通過foreach循環迭代,串聯值放入一個INSERT查詢(有關詳細信息,請參閱http://dev.mysql.com/doc/refman/5.5/en/insert.html),並有foreach循環排除鍵「本科」,「國際」 ,「Postdoc」以及任何長度爲0的值。

您的問題似乎在INSERT查詢中。嘗試這樣的:

$query = sprintf("INSERT INTO save_array (CreditWeighting, TeachingPeriod, NoofStudents, etc.)) VALUES ('%s')",mysql_real_escape_string($snode)); 

foreach ($result as $key => $value) 
    $query = $query . "$value"; 

並做一些調整,以獲得查詢字符串恰到好處。

+0

嘿謝謝你的答覆。我的意思是多行是每一個值插入到不同的行,因爲我會這樣做的大量數據,並希望在數組中提取的所有數據分別保存。不知道我是否可以正確解釋。 – user1444442

+0

看起來你只需要鍵值對錶,但在這種情況下,你將失去很多信息。你有一個你正在使用的模式(數據庫表定義)嗎? –

+0

我將模式添加到上面編輯的代碼中。請查看它。 – user1444442

0

您可以在數組的所有元素上循環並準備SQL INSERT INTO statment,並在執行此操作時執行它。

+0

我確實嘗試過,但所有的內容都只保留在一列中。我有一個上面做過的示例代碼。 – user1444442