将特定的PHP数据保存到XML
我似乎无法使用web表单从PHP获取特定数据以输出到XML文件 我做错了什么?将特定的PHP数据保存到XML,php,xml,Php,Xml,我似乎无法使用web表单从PHP获取特定数据以输出到XML文件 我做错了什么? <?php $ip = $_POST['ip']; $httpref = $_POST['httpref']; $httpagent = $_POST['httpagent']; $visitor = $_POST['visitor']; $visitormail = $_POST['visitormail']; $notes = $_POST['notes']; $attn = $_POST['attn']
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! ");
}
$todayis = date("l, F j, Y, g:i a");
$attn = $attn ;
$subject = $attn;
$notes = stripcslashes($notes);
$message = "
Subject: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
";
$from = "From: $visitormail\r\n";
mail('my@email.com', $subject, $message, $from);
?>
<?php
$xml = new SimpleXMLElement('');
$mydata = $xml->addChild('VisitorInfo');
$mydata->addChild('Visitor',$visitor);
$mydata->addChild('Key',$ip);
$mydata->PHP_EOL;
mysql_close($db);
$fp = fopen("VisitorData.xml","wb");
fwrite($fp,$xml->asXML());
fclose($fp);
?>
我收到的错误代码:
警告:SimpleXMLElement::u-construct()[SimpleXMLElement.-construct]:实体:第2行:解析器错误:需要开始标记,来自PHP文档的第一个值必须是string类型,其数据应该是“格式良好的XML字符串或XML文档的路径或URL”
目前,您没有满足这些条件。您应该创建一个新节点,例如:
$xml = new SimpleXMLElement('<xml/>');
$mydata = $xml->addChild('VisitorInfo');
$mydata->addChild('Visitor','toto');
$mydata->addChild('Key', '1');
$mydata->PHP_EOL; // Don't understand the goal here
此外,您正在代码中使用mysql.*
。考虑切换到mysqli.*
或PDO
。来自PHP文档:
“警告:从PHP5.5.0开始,此扩展已被弃用,将来将被删除。相反,应使用MySQLi或PDO MySQL扩展。”您得到了什么?有什么错误吗?我怀疑你用$xml=newsimplexmlement(“”)能得到任何数据
@bksi我对任何形式的PHP都很陌生,因为我只真正处理过HTML和CSS,所以这对我来说有点像猜谜游戏。我似乎没有收到任何错误,我的网络表单电子邮件也很好。但是我托管的XML文件没有update@l“我对任何PHP都是新手,它是别人建议的代码。@SaturnsEye,字符串是空的,应该很明显。太棒了!终于有进展了。XML数据正在更新,以显示用户名和IP,但如何获取该数据,使其添加到XML文件中,而不是每次都覆盖它?如果您想将数据添加到现有XML文件中,也许此链接可以帮助您:。非常感谢!你建议的链接工作得很好。我也接受了你的答案,因为它确实符合我最初的要求。
$xml = new SimpleXMLElement('<xml/>');
$mydata = $xml->addChild('VisitorInfo');
$mydata->addChild('Visitor','toto');
$mydata->addChild('Key', '1');
$mydata->PHP_EOL; // Don't understand the goal here
SimpleXMLElement Object
(
[Visitor] => toto
[Key] => 1
)