I'm trying to put some code together that inserts data, whilst checking whether the data already exists into a mySQL table.
The data originates as an xml file which I've managed to extract and put into a PHP format but I'm struggling to get the 'Insert' and 'Checking' functionality working.
This is the code that I've got at the moment.
<?
$objDOM = new DOMDocument();
$objDOM->load("xmlfile.xml");
$Details = $objDOM->getElementsByTagName("Details");
foreach( $Details as $value )
{
$ListEntry = $value->getElementsByTagName("ListEntry");
$listentry = $ListEntry->item(0)->nodeValue;
$SiteType = $value->getElementsByTagName("SiteType");
$sitetype = $SiteType->item(0)->nodeValue;
$SiteDescription = $value->getElementsByTagName("SiteDescription");
$sitedescription = $SiteDescription->item(0)->nodeValue;
$Siteosgb36lat = $value->getElementsByTagName("Siteosgb36lat");
$siteosgb36lat = $Siteosgb36lat->item(0)->nodeValue;
$Siteosgb36lon = $value->getElementsByTagName("Siteosgb36lon");
$siteosgb36lon = $Siteosgb36lon->item(0)->nodeValue;
}
require("phpfile.php");
// Opens a connection to a MySQL server
$connection = mysql_connect ("hostname", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
mysql_query("INSERT INTO sitestable(listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon) VALUES($listentry, $sitetype, $sitedescription, $siteosgb36lat, $siteosgb36lon ) ")
or die(mysql_error());
echo "Data Inserted!";
?>
I'd like the query to check to see whether there is an entry in the 'listentry' column that matches data in the file I'm wanting to add, if there is, then I want it to check all the fields for that record and update accordingly into my table.
Could someone perhaps take a look at this and let me know where I'm going wrong please.
Kind regards
UPDATED CODE
<?
$objDOM = new DOMDocument();
$objDOM->load("xmlfile.xml");
$Details = $objDOM->getElementsByTagName("Details");
foreach( $Details as $value )
{
$listentry = $value->getElementsByTagName("listentry");
$listentrys = $listentry->item(0)->nodeValue;
$sitetype = $value->getElementsByTagName("sitetype");
$sitetypes = $sitetype->item(0)->nodeValue;
$sitedescription = $value->getElementsByTagName("sitedescription");
$sitedescriptions = $sitedescription->item(0)->nodeValue;
$siteosgb36lat = $value->getElementsByTagName("siteosgb36lat");
$siteosgb36lats = $siteosgb36lat->item(0)->nodeValue;
$siteosgb36lon = $value->getElementsByTagName("siteosgb36lon");
$siteosgb36lons = $siteosgb36lon->item(0)->nodeValue;
//echo "$listentrys :: $sitetypes :: $sitedescriptions :: $siteosgb36lats :: $siteosgb36lons <br>";
}
require("phpfile.php");
//Opens a connection to a MySQL server
$connection = mysql_connect ("hostname", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
mysql_query("INSERT IGNORE INTO sitestable (listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon) VALUES('$listentrys','$sitetypes','$sitedescriptions','$siteosgb36lats','$siteosgb36lons') ")
or die(mysql_error());
echo "Data Inserted!";
?>