php - I need to add information to an already existing user in an XML file -


session_start(); $xml = simplexml_load_file('gebruikers.xml'); $gebruiker = $xml->xpath('root[login="'.$_session['login'].'"]'); $coordinates = $gebruiker[0]->addchild('coordinates'); $coordinates->addchild("x",$_get["x"]); $coordinates->addchild("y",$_get["y"]); file_put_contents('gebruikers.xml', $xml->asxml()); 

xml-file

<root>     <gebruiker>         <login>tom</login>         <wachtwoord>123</wachtwoord>     </gebruiker> </root> 

getting these errors :

  • notice: undefined offset: 0 in c:\xampp\htdocs\tom\project php\projectphp_html\coordinaten.php on line 6
  • fatal error: call member function addchild() on non-object in c:\xampp\htdocs\tom\project php\projectphp_html\coordinaten.php on line 6

i'm stuck .. please me out.

the xpath $gebruiker wrong, instead:

$gebruiker = $xml->xpath('gebruiker[login="'.$_session['login'].'"]'); 

what did...

... xpath('root[login="'.$_session['login'].'"]'); 

means: find <login> node value in $_session['login'] below <root>, there no such node, below <gebruikers>.

btw, instead of file_put_contents('gebruikers.xml', $xml->asxml());

you can $xml->asxml('myfile.xml');

see working: http://codepad.viper-7.com/9p2myb


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -