vmicchia Posted December 7, 2009 Share Posted December 7, 2009 Hey there I'm having some problems with some xml and PHP I am trying to write. I know xml isn't the best to use for a database but I wanted to try something different from MySQL. Anyway I have a login xml file that Looks like this: <login> <user> <userName>YourName</userName> <pass>YourPass</pass> <type>user</type> </user> </login> I am trying to add a user node to this but my code isn't working. Everything I find out there is to make a whole new XML file. anyway here is my code I hope you can help. Thanks again. <?php include("../includes/loginConstants.php"); if(empty($_POST["user"]) || empty($_POST["type"])){ $_SESSION["errorMessage"] = "Please fill in all fields."; CleanUp(); header("Location:../addUser.php"); exit; } $userName = $_POST["user"]; $type = $_POST["type"]; $_SESSION["userName"] = $userName; $loginNode = $root->getElementsByTagName("login")->item(0); $doc->preserveWhiteSpace = false; $user = $doc->createElement("user"); echo $user; $userNameNode = $doc->createElement("userName"); $userNameNode->appendChild($doc->createTextNode($_POST["user"])); $user->appendChild($userNameNode); $passNode = $doc->createElement("pass"); $passNode->appendChild($doc->createTextNode($_POST["user"])); $user->appendChild($passNode); $typeNode = $doc->createElement("type"); $typeNode->appendChild($doc->createTextNode($_POST["type"])); $user->appendChild($typeNode); $loginNode->appendChild($user); $doc->formatOutput = true; //format the output so that its tabbed correctly //save the document $doc->save($XMLfile); header("Location:index.php"); function CleanUp(){ $user = ""; $type = ""; } ?> Oh and here is the constants includes file. <?php session_start(); $XMLfile= "../xml/login.xml"; $doc = new DOMDocument(); $doc ->load($XMLfile); $xpath = new DOMXpath($doc); $root = $doc->documentElement; ?> Link to comment https://forums.phpfreaks.com/topic/184227-help-with-writing-to-an-xml-file-with-php/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.