Jump to content

Help with writing to an XML file with PHP


vmicchia

Recommended Posts

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;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.