gpt4 book ai didi

php - 使用 PHP 、 MYSQL 编写和更新 XML 文件

转载 作者:行者123 更新时间:2023-11-29 08:54:18 25 4
gpt4 key购买 nike

我试图根据下拉列表中的用户选择在此处创建一个 XML 文件,根据我的选择;运行查询以从 phpmyadmin 获取特定数据,然后我尝试将此数据放入 XML 文件中,但我得到的只是一个空 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>

最佳答案

XML 并不是一门火箭科学。
它与 HTML 或任何其他格式的文本一样原始。
能够创建 HTML 表格的人肯定也能够创建 XML。不需要花哨的 DOM 解析器。输出简单的文本。

只需获取所需的 XML 作为示例,并以与创建 HTML 相同的方式创建它。

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

并不比

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

关于php - 使用 PHP 、 MYSQL 编写和更新 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10370652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com