prash91 Posted March 7, 2014 Share Posted March 7, 2014 I have two php file createxmlfile.php and display.php I am trying to append the text which i insert through display.php file and create a new xmlfile call phpxml1.php everything works perfectly, except it also add duplicate date, I need a hint, how can i avoid inserting duplicate data <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Create Xml File In PHP</title></head><body><p>Please fill following Data</p><Form action="display.php" method="post">ID :<input type="text" id="id" name="id" />Name : <input type="text" id="name" name="name" /> <input type="submit" id="submit" value="Submit"></Form></body></html> display.php <?PHPfunction c_element($e_name,$parent){ global $xml; $node=$xml->createElement($e_name); $parent->appendChild($node); return $node; }function c_value($value,$parent){ global $xml; $value = $xml->createTextNode($value); $parent->appendChild($value); return $value; } ?> <?PHP$s_id = $_POST['id'];$s_name = $_POST['name']; echo $s_id. '<br>' .$s_name;$xml =new DOMDocument("1.0");$xml->load("xmlphp1.xml"); $root=$xml->getElementsByTagName("students")->item(0);$student=c_element("student",$root); $id=c_element("id", $student);c_value("$s_id",$id); $name = c_element("name", $student);c_value("$s_name",$name); //$xml= new DOMDocument("1.0","utf-8");//$employee = $xml->createElement("employee");//$employee = $xml->appendChild($employee); //$empname = $xml->createElement("empname",$name);//$empname = $employee->appendChild($empname); //$empemail = $xml->createElement("empemail",$email);//$price= $employee->appendChild($empemail); $xml->FormatOutput=true;//$string_value=$xml->saveXML();$xml->save("xmlphp1.xml"); ?> Quote Link to comment Share on other sites More sharing options...
prash91 Posted March 8, 2014 Author Share Posted March 8, 2014 Well, the data what i insert here from form which, is createxmlfile.php and display.php makes new xml file called xmlphp1.xml it gives me the results for example <?xml version="1.0" encoding="utf-8"?><students> <student> <id>123</id> <name>Paul</name> </student> <student> <id>124</id> <name>Sandy</name> </student></students> so I have these data in my xml file if I try inserting same data let say Paul or 123 it should show me the message that the name is already exists. Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted March 8, 2014 Solution Share Posted March 8, 2014 (edited) xml is designed/intended to be used as a data exchange format between different systems. it is not designed to be a database. all the extra code and processing needed to treat is as a database, and in your case to prevent adding duplicate data, is not efficient. in order to prevent duplicated data, you would need to search/parse through the "xmlphp1.xml" values and test to make sure that the newly submitted data isn't already present. unless your assignment is to devise how to find duplicate data using xml, an alternate approach would be to store the actual data in a database or at a minimum a serialized/json array stored in a file, so that php can easily read it into a native php array and process it. testing/preventing duplicates would occur either in a database query or using php's array functions. you would then simply iterate over the actual data and output it as xml only when needed. Edited March 8, 2014 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.