【问题标题】:XML DOM, PHP sending 1's to XML?XML DOM,PHP向XML发送1?
【发布时间】:2015-10-24 10:35:52
【问题描述】:

想不通这个。我的 PHP 看起来不错,留下的注释掉的代码生成了 2 个 XML 数据集。 XML 的输出在每个元素下都是“1”……为什么?

PHP:
<?php 
echo "Submission Successful!";
echo "<a href='buyonline.htm'>Back</a>";

$dom = new DOMDocument();
$dom->load("data/customer.xml");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

$custid = uniqid();
$firstname = isset($_POST['firstname']);
$lastname = isset($_POST['lastname']);
$email = isset($_POST['email']);
$password = isset($_POST['password']);
$phone = isset($_POST['phone']);

/*$firstname = htmlentities($firstname, ENT_COMPAT, 'UTF-8', false);
$lastname = htmlentities($lastname, ENT_COMPAT, 'UTF-8', false);
$email = htmlentities($email, ENT_COMPAT, 'UTF-8', false);
$password = htmlentities($password, ENT_COMPAT, 'UTF-8', false);
$phone = htmlentities($phone, ENT_COMPAT, 'UTF-8', false);*/

$customers = $dom->getElementsByTagName("customers")->item(0);

    $customertag = $dom->createElement("customer");

        $custidtag = $dom->createElement("custid", $custid);
        $firstnametag = $dom->createElement("firstname", $firstname);
        $lastnametag = $dom->createElement("lastname", $lastname);
        $emailtag = $dom->createElement("email", $email);
        $passwordtag = $dom->createElement("password", $password);
        $phonetag = $dom->createElement("phone", $phone);

    $customertag->appendChild($custidtag);
    $customertag->appendChild($firstnametag);
    $customertag->appendChild($lastnametag);
    $customertag->appendChild($emailtag);
    $customertag->appendChild($passwordtag);
    $customertag->appendChild($phonetag);


$customers->appendChild($customertag);

$dom->save('data/customer.xml');


?>

XML: 
<?xml version="1.0"?>
<customers>
<customer>
    <custid>
    </custid>
    <firstname>
    </firstname>
    <lastname>
    </lastname>
    <email>
    </email>
    <password>
    </password>
    <phone>
    </phone>
</customer>
<customer><custid>562b5d237b599</custid><firstname>1</firstname>    
<lastname>1</lastname><email>1</email><password>1</password><phone>1
</phone></customer></customers>

如果你们需要,我可以发布 HTML 和 JS。请帮忙:)

【问题讨论】:

    标签: javascript php xml


    【解决方案1】:

    函数 isset 返回 truefalse,取决于是否设置了变量。

    执行$phone = isset($_POST['phone']) 意味着$phone 获得价值truefalse。将布尔值true 写入文件以将其转换为1 结束。

    我想你想检查是否设置了值,如果没有设置一些默认值。你可以这样做:

    $phone = isset($_POST['phone'])? $_POST['phone'] : 'DEFAULT';
    

    你可以用任何值代替'DEFAULT'

    更新: 关于注释代码。不确定您是否应该使用它。但是这段代码的情况是下一个:假设有人输入他的姓氏为I am &lt;robot/&gt;。当您将其直接放入您的 xml 文件时,您将收到如下内容:

    <lastname>I am <robot/></lastname>
    

    看,你会得到另一个 xml 标记 robot,它位于 lastname 内部。意外!这就是为什么你应该逃避你的价值观。 但我不确定 - 您是否应该隐式执行,或者 DOM 库在导出到 xml 文件时也执行此操作。所以你可以自己试试看。

    【讨论】:

    • 非常感谢!!完美运行!你知道我是否需要使用被注释掉的代码,它被推荐但我不知道为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多