I am fairly new to php and xml and followed through a tutorial on creating a guestbook found here: http://www.sephiroth.it/tutorials/f...omxml/index.php ) Well, it seems to have a bug in it that I can't fix. I had sent the author an email but got no reply. The problem is it will write to the file called "guestbook.xml" but after (2) entries it seems to overwrite all the entries with the previous entry; if that makes any sense.
Example:
After the 1st entry the result would be:
Name: 1st entry
Email: 1st_entry@blah.com
Message: 1st entry message
(this works fine)
After the 2nd entry the result would be:
Name: 2nd entry
Email: 2nd_entry@blah.com
Message: 2nd entry message
Name: 1st entry
Email: 1st_entry@blah.com
Message: 1st entry message
(this still is fine)
However, after the 3rd entry the result will look like this:
Name: 3rd entry
Email: 3rd_entry@blah.com
Message: 3rd entry message
Name: 2nd entry
Email: 2nd_entry@blah.com
Message: 2nd entry message
Name: 2nd entry
Email: 2nd_entry@blah.com
Message: 2nd entry message
--Notice how entry (1) is overwriten by entry (2) this pattern will continue for each additional entry.--
To debug the problem I downloaded the source files and it does the same thing with no changes to them.
This is the source code for the php file:
<?php
#print htmlspecialchars(utf8_encode("<@รจ"));
#die();
/*********************************************************
XML creator
Read and write xml data
using the opensource DOM based library
phpdomxml ([url]http://sourceforge.net/projects/phpdomxml[/url])
XSL tutorials:
[url]http://www.w3schools.com/xsl/xsl_languages.asp[/url]
*********************************************************/
// ----------------------------
// define the XML file resource
// ----------------------------
define("XML_FILE", "guestbook.xml");
// ----------------------------
// include the XML library
// ----------------------------
include "include/lib.xml.inc.php";
// -----------------------
// Create the MAIN NODE
// -----------------------
function createXML(){
$doc = new XML();
$doc->encoding = "UTF-8";
$doc->docTypeDecl = "<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet type='text/xsl' href='guestbook.xsl'?>";
$main_node = $doc->createElement("news");
$main_node->attributes['creator'] = "phpdomxml-0.8.5";
$doc->appendChild($main_node);
return $doc;
}
// ----------------------
// Create an ENTRY node
// ----------------------
function appendItem($sData, $sTitle, $sEmail, $sDesc, $sPlayer=""){
$mXML = new XML();
// create node elements
$entry_xml = $mXML->createElement("entry");
$author_xml = $mXML->createElement("author");
$desc_xml = $mXML->createElement("message");
$fplayer_xml = $mXML->createElement("fplayer");
$date_xml = $mXML->createElement("date");
$email_xml = $mXML->createElement("email");
$cdata_xml = $mXML->createElement("cdata");
// store values
$author_xml->appendChild($mXML->createTextNode(htmlspecialchars (utf8_encode($sTitle))));
$date_xml->appendChild($mXML->createTextNode($sData));
$fplayer_xml->appendChild($mXML->createTextNode($sPlayer));
$email_xml->appendChild($mXML->createTextNode(htmlspecialchars (utf8_encode($sEmail))));
// create the CDATA node
$cdata_xml->nodeType = XML_TYPE_CDATA;
$cdata_xml->nodeValue = $sDesc;
$desc_xml->appendChild($cdata_xml);
$entry_xml->appendChild($author_xml);
$entry_xml->appendChild($date_xml);
$entry_xml->appendChild($email_xml);
$entry_xml->appendChild($fplayer_xml);
$entry_xml->appendChild($desc_xml);
return $entry_xml;
}
// -----------------
// save XML
// -----------------
function saveXML($sFile, $sText=""){
$fp = @fopen($sFile,"w");
if($fp){
fwrite($fp, $sText);
fclose($fp);
}
}
// ---------------------
// ACTIONS (CHECK POST)
// ---------------------
if(!empty($_POST['action']) and !empty($_POST['name']) and !empty($_POST['email']) and !empty($_POST['message']) and isset($_POST['player'])){
// -------------------------
// Create the new ENTRY node
// -------------------------
$new_entry = appendItem(date("D d-m-Y H:i:s"), $_POST['name'], $_POST['email'], $_POST['message'], $_POST['player']);
// ----------------------
// create an XML instance
// ----------------------
if(is_file(XML_FILE) and is_writeable(XML_FILE) and is_readable(XML_FILE)){
// Create the parser reader
$myXML = new XML(XML_FILE);
// Create the writer XML
$writerXML = createXML();
// atach the NEW entry as first node
$writerXML->firstChild->appendChild($new_entry);
// get only the last 30 entries...
$k = 30;
for($a = count($myXML->firstChild->childNodes) - 1; $a >= 0; $a--){
$author = $myXML->firstChild->childNodes[$a]->firstChild->firstChild->nodeValue;
$date = $myXML->firstChild->childNodes[$a]->firstChild->nextSibling->firstChild->nodeValue;
$email = $myXML->firstChild->childNodes[$a]->firstChild->nextSibling->nextSibling->firstChild->nodeValue;
$player = $myXML->firstChild->childNodes[$a]->firstChild->nextSibling->nextSibling->nextSibling->firstChild->nodeValue;
$message = $myXML->firstChild->childNodes[$a]->firstChild->nextSibling->nextSibling->nextSibling->nextSibling->firstChild->nodeValue;
$old_entry = appendItem($date, $author, $email, $message, $player);
$k--;
if($k <= 0){
break;
}
$writerXML->firstChild->appendChild($old_entry);
}
saveXML(XML_FILE, $writerXML->toString());
}
} else {
if(!is_file(XML_FILE)){
// file does not exists, create a new one
$myXML = createXML();
// ------------
// save the XML
// ------------
saveXML(XML_FILE, $myXML->toString());
}
}
// ----------------------
// print out the XML file
// ----------------------
if(is_file(XML_FILE)){
readfile(XML_FILE);
}
?>
[code=php]
Any suggestions will be much appreciated. I am pulling my hair out with this one.
Thank you,
Kevin Muscott