使用PHP将表单数据保存到现有XML文件中 [英] Saving form data to an existing XML-file using PHP

查看:146
本文介绍了使用PHP将表单数据保存到现有XML文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是我想要做的(注意:我对PHP还是陌生的):

Okay, so here's what I want to do (NOTE: I'm stil new to PHP):

我有一个电影/电视剧等的注册表格.然后,将已填写表格中的数据注册(register.php)并发送到MySQL数据库,该数据库工作正常.但是,这里出现了:我还希望在同一个寄存器文件(register.php)中将数据存储在现有XML文件(data.xml)中.这里重要的是,每个成功提交的表单都存储在相同的XML文件(data.xml)中.

I have a registration form for movies/series etc. The data from the filled form is then registered (register.php) and sent to a MySQL database, which is working fine. Here comes the but: I also want, in the same register file (register.php), the data to be stored in an existing XML-file (data.xml). Important here is that every successfully submitted form is stored in the same XML-file (data.xml).

我通过一个while循环在HTML表中显示所有已注册的电影",该循环现在从我的数据库中收集数据.我也恳求在表外的某个地方添加一个按钮,以在新选项卡(?)中生成/显示XML文件的内容.不要右键单击以查看源,因为表数据是从MySQL收集的,所以这是不可能的.

I display all registered "movies" in a HTML-table through a while-loop, which now collects the data from my database. I'm also begging for help to, somewhere outside the table, add a button which generate/display the content of the XML-file in a new tab(?). Not right-clicking to view source, since the table data is collected from MySQL this is impossible.

这是我到目前为止的内容(此操作可以将每个提交保存在data.xml中,但是如果进行了另一个提交则替换为-我想添加不替换"):

Here's what I have so far (this manages to save each submission in data.xml but replaces if another submission is made - I want to add NOT replace):

首先,index.php:

First off, index.php:

<form enctype="multipart/form-data" action="core/register.php" method="post" autocomplete="true">
    <p><input type="text" name="name" placeholder="Program name" /></p>
    <p><input type="date" name="date" placeholder="Program date" /></p>
    <p><input type="time" name="time" placeholder="Program time" /></p>
    <p><input type="text" name="bline" placeholder="B-line" /></textarea></p>
    <p><textarea name="synopsis" placeholder="Program synopsis" /></textarea></p>
    <p><textarea name="leadtext" placeholder="Lead text" /></textarea></p>
    <p><input type="url" name="url" placeholder="URL" /></p>
    <p><input type="submit" value="Register" name="register" /></p>
</form>

接下来,data.xml:

Next up, data.xml:

<?xml version="1.0" encoding="UTF-8"?>
<programs>
    <program>
        <name></name>
        <date></date>
        <start_time></start_time>
        <b-line></b-line>
        <synopsis></synopsis>
        <leadtext></leadtext>
        <url></url>
    </program>
</programs>

最后,register.php:

Finally, register.php:

require_once('db.php');

$str = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="xsl.xsl"?><programs></programs>';
$xml = simplexml_load_string($str);

$name = $_POST['name'];
$date = $_POST['date'];
$time = $_POST['time'];
$bline = $_POST['bline'];
$synopsis = $_POST['synopsis'];
$leadtext = $_POST['leadtext'];
$url = $_POST['url'];

$name = htmlentities($name, ENT_COMPAT, 'UTF-8', false);
$date = htmlentities($date, ENT_COMPAT, 'UTF-8', false);
$time = htmlentities($time, ENT_COMPAT, 'UTF-8', false);
$bline = htmlentities($bline, ENT_COMPAT, 'UTF-8', false);
$synopsis = htmlentities($synopsis, ENT_COMPAT, 'UTF-8', false);
$leadtext = htmlentities($leadtext, ENT_COMPAT, 'UTF-8', false);
$url = htmlentities($url, ENT_COMPAT, 'UTF-8', false);

$xml->program->addChild('name', $name);
$xml->program->addChild('date', $date);
$xml->program->addChild('start_time', $time);
$xml->program->addChild('b-line', $bline);
$xml->program->addChild('synopsis', $synopsis);
$xml->program->addChild('leadtext', $leadtext);
$xml->program->addChild('url', $url);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('data.xml');

我已经搜索了几个小时,却没有找到我想要的东西.还尽我所能尝试很多我自己的非工作解决方案".此外,我还选择了与该问题答案相关的代码部分.

I have searched for hours without finding exactly what I'm looking for. Also tried my best to try plenty of my own "not-working-solutions". Also, I picked out the parts of my code that's relevant for answers on this matter.

推荐答案

您需要通过为其分配值来使program节点成为永久:

You need to make the program node permanent by assigning it a value like so:

$xml->program = "";

在添加子节点之前添加该行:

Add that line just before adding child nodes to it:

$xml->program = "";
$xml->program->addChild('name', $name);
$xml->program->addChild('date', $date);
....

这篇关于使用PHP将表单数据保存到现有XML文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆