Jason Stewart
Jason Stewart

Reputation: 15

DOMDocument error when trying to save date in xml file - php script

Having a little problem with saving form data to xml. I've tried adding new element ($ukey) to xml object, but it gives me an error on line with $ukey variable. $newItem->appendChild($xml->createElement('ukey', $ukey));

// Script by Fred Fletcher, Canada.
$fname = $_POST['name'];
$lname = $_POST['email'];
$location = $_POST['cat'];
$report = $phone;
$description = $_POST['content'];
$ukey = date("dmYHis");


$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('/home/mysite/public_html/file.xml');

$element = $xml->getElementsByTagName('reports')->item(0);

$timestamp = $element->getElementsByTagName('timestamp')->item(0);
$fname = $element->getElementsByTagName('fname')->item(0);
$lname = $element->getElementsByTagName('lname')->item(0);
$location = $element->getElementsByTagName('location')->item(0);
$report = $element->getElementsByTagName('report')->item(0);
$description = $element->getElementsByTagName('description')->item(0);
$ukey = $element->getElementsByTagName('ukey')->item(0);

$newItem = $xml->createElement('reports');

$newItem->appendChild($xml->createElement('timestamp', date("F j, Y, g:i a",time())));;

$newItem->appendChild($xml->createElement('fname', $_POST['name']));
$newItem->appendChild($xml->createElement('lname', $_POST['email']));
$newItem->appendChild($xml->createElement('location', $_POST['cat']));
$newItem->appendChild($xml->createElement('report', $phone));
$newItem->appendChild($xml->createElement('description', $_POST['content']));
$newItem->appendChild($xml->createElement('ukey', $ukey));

$xml->getElementsByTagName('entries')->item(0)->appendChild($newItem);

$xml->save('/home/mysite/public_html/file.xml');

Here is the error

Warning: DOMDocument::createElement() expects parameter 2 to be string, object given in /home/mysite/public_html/wp-content/themes/twentytwelve/myform.php on line 214

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /home/mysite/public_html/wp-includes/template-loader.php on line 47 and defined in /home/mysite/public_html/wp-content/themes/twentytwelve/myform.php on line 214

And xml sample

<?xml version="1.0" encoding="UTF-8"?>
<entries>
  <reports>
   <timestamp>September 19, 2013, 11:45 am</timestamp>
   <fname>John Snider</fname>
   <lname>mailATemail.com</lname>
   <location>156</location>
   <report>08974545153</report>
   <description>jhsdhfsdfgsdtasrgsfgasf</description>
   <ukey>156152</ukey>
  </reports>
</entries>

Do I need to change it to string or what is the problem?

Just to clarify, script was working great before I added $ukey

Thanks

Upvotes: 0

Views: 1323

Answers (1)

C3roe
C3roe

Reputation: 96382

$ukey = date("dmYHis");

Yes, that should be a nice string value containing a formatted date, fine.

$ukey = $element->getElementsByTagName('ukey')->item(0);

Oops, what are you doing here …?

$newItem->appendChild($xml->createElement('ukey', $ukey));

Now, $ukey is not a string value any more, because of the line quoted before – you messed up with your variable names, dude.

Upvotes: 2

Related Questions