dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP writing extra XML data

  1. #1
    Join Date
    Jul 2010
    Posts
    6

    PHP writing extra XML data

    Hi,

    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>


    Thank you.

  2. #2
    Join Date
    Aug 2010
    Location
    Dubai UAE
    Posts
    54
    I don see any error on you code,
    Check the xml file if it already has contents,
    Check you form submit if it submit twice.

    If you can add your html, that would be better ^_^

  3. #3
    Join Date
    Aug 2010
    Posts
    7
    want konw your datebase!mysql?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles