0

So heres my problem! I am using simplexml in my website as a substitute for MySQL because I'm using a mobile, its working fine with the addChild request but when it submits it either overrides the original xml file or well il explain it with code xml file:

<?xml version="1.0"?>
<video><title>title 22</title></video>

Php file:

<?php
$file = 'database.xml';
$title = $_POST['1'];
$xml = simplexml_load_file($file);
$xmlne = $xml->videos;
$d = "<video></video>";
 $newvid = new SimpleXMLElement($d);
$newvid->addChild('title' , $title);
$newvid->asXML($file);
?>
<form action="index.php" method="post">
 <input type="text" value="title" name="1">
<input type="submit" value="post"/>
</form>
4
  • Say if i put title 23 into the form it overrides <title> to 23 Commented Oct 30, 2013 at 13:05
  • "il explain it with code xml file": Better explain it with words, then show the code to reproduce it. I don't really understand "it either overrides the original xml" and I don't want to have to execute it in my brain just in order to understand the question! Commented Oct 30, 2013 at 13:26
  • The data saves but i want it to increment so it adds a new element under the others Commented Oct 30, 2013 at 13:33
  • It's recommended to edit your question to include missing information rather than only mentioning it in comments. Commented Oct 30, 2013 at 15:19

1 Answer 1

2

The problem you have is that this line creates a new XML document, completely separate from the original file:

$newvid = new SimpleXMLElement($d);

You are then adding an element to your new document, and saving out to the original file name, losing the original file.

What you actually want to do is add a new Video element to the existing document which you loaded as $xml and then traversed to find $xmlne:

$newvid = $xmlne->addChild('video');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.