How to add new content in XML using php
In index.php add the following form for inserting new data in xml using php
Then create booklistaction.php and add the following code in this file:
<?php
$book = array(
'bid' => $_POST['bid'],
'btitle' => $_POST['btitle'],
'bdate' => $_POST['bdate'],
'bookrentdate' => $_POST['bookrentdate'],
'booksid' => $_POST['booksid'],
);
echo $book["bid"];
$doc = new DOMDocument();
$doc->load( 'loaned.xml' );
$booklist = $doc->getElementsByTagName( "book" );
foreach( $booklist as $book1 )
{
$bookids = $book1->getElementsByTagName( "ID" );
$bookid = $bookids->item(0)->nodeValue;
}
echo "<b>$bookid<br>";
$bookid=$bookid+1;
$book["bid"] = $bookid;
echo "<b>$bookid<br>";
$doc->formatOutput = true;
$r = $doc->getElementsByTagName("resource")->item(0);
$b = $doc->createElement("book");
$bid = $doc->createElement("ID");
$bid->appendChild(
$doc->createTextNode( $book["bid"] )
);
$b->appendChild( $bid );
$btitle = $doc->createElement("title");
$btitle->appendChild(
$doc->createTextNode( $book["btitle"] )
);
$b->appendChild( $btitle );
$bdate = $doc->createElement("date");
$bdate->appendChild(
$doc->createTextNode( $book["bdate"] )
);
$b->appendChild( $bdate );
$bookrentdate = $doc->createElement("retdate");
$bookrentdate->appendChild(
$doc->createTextNode( $book["bookrentdate"] )
);
$b->appendChild( $bookrentdate );
$booksid = $doc->createElement("SID");
$booksid->appendChild(
$doc->createTextNode( $book["booksid"] )
);
$b->appendChild( $booksid );
$r->appendChild( $b );
$doc->save("loaned.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
</body>
Comments