【发布时间】:2014-04-22 09:26:05
【问题描述】:
我是 Lighty,一个新的程序员和学生,我现在正在学习一个小教程,读取一个 XML 文件并用 PHP 显示它,我自己想做的一件事就是将数据写入我的 XML 文件,来自一个 PHP 脚本,使用 HTML 中的表单,我的脚本和表单如下所示:
<?php
echo ('Script started');
///making sure the script only works when posted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo ('-Post accepted');
//Load XML File into variable
$xml = simplexml_load_file("phptest3.xml");
echo ('-XML Loaded');
//Connect form to Variables
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$comment = $_POST['comment'];
echo ('-Vars connected');
//Function to strip items that are not needed to prevend XSS/Injections
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
echo ('-Injections stripped');
//Create new children in XML file and Connect the from data to the corresponding XML entries
$xml->people[0];
$xml->people->addChild('person');
$xml->person[0];
$xml->person->addChild('name', $name);
$xml->person->addChild('age', $age);
$xml->person->addChild('sex', $sex);
$xml->person->addChild('comment', $comment);
echo ('-Data inserted');
//Save current data to XML file...
$xml->savexml('phptest3.xml');
echo ('-saved');
}
echo ('-Script ended.');
?>
虽然我的 XML 看起来像这样
<?xml version="1.0"?>
<people>
<person>
<name>Lighty</name>
<age>17</age>
<sex>M</sex>
<comment>iets</comment>
<name>iets</name><age>75</age><sex>F</sex><comment>igjk</comment> <name>iets</name><age>75</age><sex>M</sex><comment>gergsrh</comment></person>
</people>
现在,它现在确实插入了数据,但它把它放在了不应该放的地方:/
【问题讨论】: