I am not sure if this qualifies as a PHP problem or XML problem. I have a problem with the following program duplicating XML data:
<?php
// ----------BUILD XML FILE WITH DATA ENTERED INTO FORM----------
$filename = "inventory.xml";
if (file_exists($filename)) {
$xml = simplexml_load_file($filename);
// Add the new entry as a child node
$entry = $xml->addChild("entry");
$entry->addChild("img_txt", $_POST["img_txt"]);
$entry->addChild("title", stripslashes($_POST["title"]));
$entry->addChild("description", stripslashes($_POST["description"]));
$entry->addChild("price", stripslashes($_POST["price"]));
$entry->addChild("vin", stripslashes($_POST["vin"]));
// Write the entire entry to the file
$fh = fopen($filename, 'r+') or die("cannot open file");
fwrite($fh, $xml);
fclose($fh);
echo("XML file written!" . "<br/>");
} else {
exit('Failed to open inventory.xml.');
}
?>
My user enters info into a form, and once they hit submit, this program is run to add the data to an XML file. My problem is that it is writing it twice. As you see, the code above adds a child called "entry" to the XML. When I view the updated XML file, I get this:
<entry>
information entered from form goes here.
</entry>
<entry>
This is an explicable extra child added to the file (with none of the form data btw). Why is it here?
</entry>
Bookmarks