Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

sql - Saving form data to an existing XML-file using PHP

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

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).

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.

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):

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>

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...