2012-04-29 55 views
0

我試圖根據用戶選擇從下拉列表 中創建一個XML文件,具體取決於選擇I; m運行查詢以獲取特定數據從phpmyadmin然後我試圖把這個數據在XML文件,但我得到的是一個空的XML文件!使用PHP,MYSQL編寫和更新XML文件

另外,我想知道如何使用PHP在XML標記中添加描述? 像這樣的例如

<Exam Number="2" Date="3/7/1433" Time="2 hour" Start="8:30 am" /> 

PHP代碼:

<?php 
$connectdb = mysql_connect('localhost','root','sara') or die ("Not Connect"); 
if (!$connectdb) die('Could not connect :'. mysql_errno()); 
echo "<div align='center' style='direction: ltr' style='position: relative'> 
Choose the exam ID that you want to create it: <br /> 
    <form action='createxam.php' method='post'> <select name=\"examID\"> 
    <option value=\"0\">Exam ID </option> \n"; 
    $selestdb = mysql_select_db('iexa', $connectdb) or die ("not selected database"); 
    $qu = mysql_query("SELECT E_No FROM question_bank ") or die ("mysql error"); 
    while ($row = mysql_fetch_assoc($qu)) 
    echo "<option value=\"{$row["E_No"]}\">{$row["E_No"]}</option>\n"; 
    $examID = $_REQUEST['examID']; 
    echo "</select> </div> <br /> "; 

    echo "The date of the exam : <textarea name:'Date'></textarea><br /> 
    It will start on (In 24 hours format) : <textarea name:'Start'></textarea><br /> 
    The time of the exam : <textarea name:'Time'></textarea><br />"; 
    echo "<div align='center' style='direction: ltr' style='position: relative'>   <input type='submit' value='Create Exam' /> 
    </form></div>"; 
    mysql_close($connectdb); 
    ?> 

,寫入文件:

 <?php 
     $connectdb = mysql_connect('localhost','root','sara', true) or die ("Not Connect"); 
    if (!$connectdb) 
    { 
     die('Could not connect :'. mysql_errno()); 
    } 
    $selestdb = mysql_select_db('iexa', $connectdb) or die ("not selected database"); 

    $Date = array(); 
    $Start = array(); 
    $Time = array(); 
    $Date['Date']= mysql_real_escape_string($_POST['Date']) ; 
    $Time['Time']= mysql_real_escape_string($_POST['Time']) ; 
    $Start['Start']= mysql_real_escape_string($_POST['Start']) ; 

    if (isset($_POST['examID'])) { 
     $examID = $_POST['examID']; 
     } 

    $query ="INSERT INTO exam (Exam_Number ,Date ,Start ,Time) 
    VALUES 
     ('$examID', '{$Date['Date']}','{$Start['Start']}','{$Time['Time']}') 
     "; 
     $cors =mysql_query("SELECT C_ID FROM question_bank WHERE E__No = '$examID'"); 
     $upd ="UPDATE exam SET C_ID=$cors 
     WHERE Exam_Number='$examID'"; 
     $Exams=mysql_query("SELECT * FROM question_bank WHERE E_No=$examID "); 

     $doc = new DOMDocument(); 
     $doc->formatOutput = true; 
     $r = $doc->createElement("Exams"); 
     $doc->appendChild($r); 
       $sql=mysql_query("SELECT * FROM question_bank WHERE E_No=$examID "); 
     while ($col = mysql_fetch_assoc($sql)){ 
     foreach($Exams as $Exam) 
     { 
     $b = $doc->createElement("Exam"); 

     $E_No = $doc->createElement("E_No"); 
     $E_No->appendChild(
     $doc->createTextNode($Exam['E_No']) 
       ); 
     $b->appendChild($E_No); 

       $C_ID = $doc->createElement("C_ID"); 
     $C_ID->appendChild(
     $doc->createTextNode($Exam['C_ID']) 
     ); 
     $b->appendChild($C_ID); 

     $Question = $doc->createElement("Question"); 
     $Question->appendChild(
     $doc->createTextNode($Exam['Question']) 
     ); 
     $b->appendChild($Question); 

     $r->appendChild($b); 
     } 
    } 

    echo $doc->saveXML(); 
     $doc->save("mytry.xml"); 

    if (!mysql_query($sql,$connectdb)) 
      { 
        die ('Error :'.mysql_error()); 
      } 
    echo "The Exam is created !!"; 
    echo ' <br /> 
    <a href="exam-xml.php" >Create Another Exam</a> <br /> 
    <a href="Instructor.htm">Home</a> 
    '; 
    mysql_close($connectdb); 
    ?> 

這裏是我要創建的XML文件:

<?xml version="1.0"?> 
<Exams> 

<Exam> 
    <Exam Number="1" Date="21/6/1433" Time="1 hour" Start="10:00 am" /> 
    <Course Name="Graduation Project" Code="CS 492" Credit="3" /> 
    <Questions ID="1" Type="Multiple-choice" Question="Who has to complete the  Graduation Project?" Choice1="All Student." Choice2="Some student." Choice3="Teacher."  Choice4="Doctor." Correct="All Student." /> 
    <Questions ID="2" Type="Multiple-choice" Question="When do students begin to work on the Graduation Project?" Choice1="After 2 years from studing." Choice2="Last year." Choice3="High schools" Choice4="Befoer graduation year." Correct="High schools" /> 
    <Student ID="2853641" Name="Maram Abdullah" password="910" /> 
    <Student ID="2853615" Name="Maha Al-soyan" password="911" /> 
</Exam> 

<Exam> 
    <Exam Number="2" Date="3/7/1433" Time="2 hour" Start="8:30 am" /> 
    <Course Name="Computer Graphics" Code="CS 447" Credit="3" /> 
    <Questions ID="1" Type="Multiple-choice" Question="........ may be defined as a pictorial representation or graphical representation of objects in a computer." Choice1="GUI" Choice2="Computer graphics." Choice3="represent graphics" Choice4="Computer representation." Correct="Computer graphics." /> 
    <Questions ID="2" Type="Multiple-choice" Question="What are the advantages of laser printers?" Choice1="High speed, precision and economy." Choice2="Cheap to maintain." Choice3="Quality printers." Choice4="All of the above." Correct="All of the above." /> 
    <Student ID="2853611" Name="Ro'a Al-turki" password="912" /> 
    <Student ID="2850742" Name="Sara Al-hejily" password="913" /> 
</Exam> 

</Exams> 
+0

更復雜的你'的foreach($考試爲$考試)'但無處前面的代碼實際上,你創建一個變量'$ Exams',所以這個循環發生0次...在一個循環內把來自一個查詢的行放入一個你從不使用的變量'$ col'中。學習一次做一件事......展示表單,查詢數據庫並構建文檔。當你不知道如何或者你不知道失敗的位置時,不要試圖一次全部完成。 – 2012-04-29 08:10:42

+0

[簡單的程序到xml文件的CRUD節點和節點值]的可能的重複(http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of -xml-file) – hakre 2012-04-29 08:12:57

+0

你特別的問題是什麼? – 2012-04-29 08:40:17

回答

2

XML是否火箭科學。
它與HTML或任何其他格式化文本一樣原始。
一個能夠創建HTML表格的人肯定也能夠創建XML。 一個不需要花哨的DOM解析器。輸出簡單的文本。

只需獲取所需的XML作爲示例,並使用創建HTML的方式創建它。

echo "<exam>".htmlspecialchars($exam)."</exam>\n"; 

不超過

echo "<td>".htmlspecialchars($exam)."</td>\n";