Hello Friends, I'm pulling data from an xml file and updating it to my mysql databse. The details are bellow. I'm puling data from info.xml file <?xml version="1.0" encoding="ISO-8859-1"?> <dataxml> <data> <id>01</id> <name>Name1</name> </data> <data> <id>02</id> <name>Name2</name> </data> </dataxml> <?php // pull data from xml file $xml = new DOMDocument(); $xml->load("info.xml"); $details = $xml->getElementsByTagName("data"); foreach( $details as $value ) { $ids = $value->getElementsByTagName("id"); $id_xml = $ids->item(0)->nodeValue; $names = $value->getElementsByTagName("name"); $name_xml = $names->item(0)->nodeValue; // add information to database $query = "INSERT INTO detials (id, name) VALUES ('$id_xml', '$name_xml')"; $result = mysql_query($query, $connection) or die(mysql_error()); } ?> Its working fine but I want to validate the data if it already exists in DB. If it exists, the row will not be inserted. I used, $query = "SELECT * From details"; $result = mysql_query($query, $connection) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $id = $row["id"]; $name = $row["name"]; if ($id != $id_xml){ // add information to database $query = "INSERT INTO detials (id, name) VALUES ('$id_xml', '$name_xml')"; $result = mysql_query($query, $connection) or die(mysql_error()); } else{ echo 'data already exists!'; } } but didn't worked. I googled and found the answer to use multiple conditions in foreach but I was unable to do that. So could you help me fix the above problem? Thanx
Try: <?php $xml = new DOMDocument(); $xml->load("info.xml"); $details = $xml->getElementsByTagName("data"); foreach( $details as $value ) { $ids = $value->getElementsByTagName("id"); $id_xml = $ids->item(0)->nodeValue; $names = $value->getElementsByTagName("name"); $name_xml = $names->item(0)->nodeValue; $result = mysql_query("SELECT * FROM details WHERE id = '$id_xml'"); if(mysql_num_rows($result) == 0) { $query = "INSERT INTO details (id, name) VALUES ('$id_xml', '$name_xml')"; $result = mysql_query($query, $connection) or die(mysql_error()); } else { echo 'data already exists'; } } ?> PHP: