【问题标题】:PHP/XML - Fixing writing script [closed]PHP/XML - 修复编写脚本 [关闭]
【发布时间】: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>

现在,它现在确实插入了数据,但它把它放在了不应该放的地方:/

【问题讨论】:

    标签: php html xml file-io


    【解决方案1】:

    没有像 save() 这样的 simplexml 方法。您可以改用 saveXML。

    $_POST['name'] = 'testName';
    $_POST['age'] = '15';
    $_POST['sex'] = 'F';
    $_POST['comment'] = 'test comment';
    //Load XML File into variable
    $xml = simplexml_load_file("test.xml");
    
    //Connect form to Variables 
    $name = $_POST['name'];
    $age = $_POST['age'];
    $sex = $_POST['sex'];
    $comment = $_POST['comment'];
    
    //Create new children in XML file and Connect the from data to the corresponding XML entries    
    $xml->program = "";
    $xml->program->addChild('name', $name);
    $xml->program->addChild('age', $age);
    $xml->program->addChild('sex', $sex);
    $xml->program->addChild('comment', $comment);
    
    //Save current data to XML file...
    $xml->saveXML('test.xml');
    

    test.xml:

    <?xml version="1.0"?>
    <people>
    
    <person>
        <name>Lighty</name>
        <age>17</age>
        <sex>M</sex>
        <comment>eyayayayaya</comment>
    </person>
    
    <program><name>testName</name><age>15</age><sex>F</sex><comment>test comment</comment></program></people>
    

    编辑:

    你说你有一个错误,你为什么不发布它?因为当我试图执行你的脚本时,我得到了一个很明显的错误Fatal error: Call to undefined method SimpleXMLElement::save()

    【讨论】:

    • 对不起,这仍然是新手,实际上,它在使其 saveXML 后停止给出错误,现在唯一的问题是,它显示为编辑,但仅显示为“临时”,喜欢它没有写入权限,现在正在努力获取写入权限的文件权限
    • 您所说的“编辑,但仅作为“临时”是什么意思?你能提供一些数据/例子吗?
    • 好吧,我现在已经修复了那部分,现在的问题是它在 people 孩子中添加了数据,XML 看起来如下: blablabla 它应该在 person 的结束标签之后添加一个新的 person 标签,但它没有,它只是在第一个 person 标签下的最后一个标签之后添加一个新的 person 标签,并为人和 person 添加一个额外的结束标签,甚至没有创建开始标签
    • 如果你只是发布新的 xml 数据会容易得多。
    • 用新脚本和 xml 编辑了问题
    猜你喜欢
    • 2011-08-27
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2014-02-11
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多