如何使用PHP将数据更新/附加到XML文件中?
我是PHP新手。我正在尝试从HTML表单值创建一个XML文档 以下是PHP代码:如何使用PHP将数据更新/附加到XML文件中?,php,html,xml,xmldom,Php,Html,Xml,Xmldom,我是PHP新手。我正在尝试从HTML表单值创建一个XML文档 以下是PHP代码: <?php if (isset($_POST['lsr-submit'])) { header('Location: http://movie1b.tk'); } $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>'; $xml = simplexml_
<?php
if (isset($_POST['lsr-submit']))
{
header('Location: http://movie1b.tk');
}
$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];
$fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
$lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
$location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
$report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
$description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);
$xml->reports = "";
$xml->reports->addChild('fname', $fname);
$xml->reports->addChild('lname', $lname);
$xml->reports->addChild('location', $location);
$xml->reports->addChild('report', $report);
$xml->reports->addChild('description', $description);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test2.xml', 'a');
?>
reports=”“;
$xml->reports->addChild($fname',$fname);
$xml->reports->addChild('lname',$lname);
$xml->reports->addChild('location',$location);
$xml->reports->addChild('report',$report);
$xml->reports->addChild('description',$description);
$doc=新DOMDocument('1.0');
$doc->formatOutput=true;
$doc->preserveewhitespace=true;
$doc->loadXML($xml->asXML(),LIBXML\u NOBLANKS);
$doc->save('test2.xml','a');
?>
下面是HTML代码:
<form name="lsrReports" action="test.php" method="post">
<table width="50%" align="center" cellpadding="2" cellspacing="0">
<tr>
<td> First name:</td><td> <input type="text" name="firstname"></td>
<td> Last name:</td><td> <input type="text" name="lastname"></td>
</tr>
<tr>
<td> Location:</td><td> <input type="text" name="location"></td>
<td> Report:</td><td> <select name="report">
<option value="Wind Damage" selected>Wind Damage</option>
<option value="Hail">Hail</option>
<option value="Flooding">Flooding</option>
<option value="Power Outage">Power Outage</option>
<option value="General">General</option>
</select>
</td>
</tr>
<tr>
<td> Description: </td><td colspan="4"> <textarea rows="5" cols="65" name="desc" onfocus="this.value=''">Enter report description</textarea></td>
</tr>
<tr>
<td colspan="4" style="text-align:center;"><input type="submit" name="lsr-submit" value="Submit"></td>
</tr>
</table>
</form>
名字:
姓氏:
地点:
报告:
风害
冰雹
泛滥的
停电
一般的
说明:输入报告说明
每次我按下submit按钮,它都会覆盖现有的XML。我想知道如何在不覆盖XML的情况下保存用户值,如果文件已经存在,我希望它将用户值添加到现有XML的顶部
谢谢大家。不要每次都创建新的XML文件,而是要加载现有的XML文件。要尽可能多地重用代码,请使用SimpleXML的文件读取功能:
// This file "test.xml" is pre-populated with your base XML:
// <?xml version="1.0" encoding="UTF-8"?><entrys></entrys>
$xml = simplexml_load_file('test.xml');
运行两次,我们将得到如下结果:
<?xml version="1.0" encoding="UTF-8"?>
<entrys>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
</entrys>
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
但它不会在text2.xml的开头保存数据。而是保存在按钮处。。我希望每次更新时都能在开始时保存数据谢谢
<?xml version="1.0" encoding="UTF-8"?>
<entrys>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
</entrys>