[英]Insert & Update MySQL Table
我正在尝试将一些插入数据的代码放在一起,同时检查数据是否已存在于mySQL表中。
数据源自一个XML文件,我设法将其提取并放入PHP格式,但是我正努力使“插入”和“检查”功能正常工作。
这是我目前所拥有的代码。
<?
$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!";
?>
我希望查询检查“ listentry”列中是否有一个条目与我要添加的文件中的数据匹配,如果存在,那么我希望它检查该记录的所有字段并相应地更新到我的表格中
也许有人可以看看这个,让我知道我要去哪里了。
亲切的问候
更新的代码
<?
$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!";
?>
如果listentry属性是表的主键或已定义唯一索引,则可以使用INSERT ... ON DUPLICATE KEY UPDATE命令,有关更多详细信息,请参见http://dev.mysql.com/doc/refman /5.0/en/insert-on-duplicate.html 。
如前所述, INSERT .. ON DUPLICATE KEY UPDATE
是最好的选择。 对于您的特定查询,它看起来像这样:
INSERT INTO sitetable
(listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon)
VALUES
($listentry, $sitetype, $sitedescription, $siteosgb36lat, $siteosgb36lon)
ON DUPLICATE KEY UPDATE
listentry = VALUES(listentry),
sitetype = VALUES(sitetype),
sitedescription = VALUES(sitedescription),
siteosgb36lat = VALUES(siteosgb36lat),
siteosgb36lon = VALUES(siteosgb36lon);
假设您要使用所有新值,这听起来很像,并且其中一个新值在 UNIQUE
列中使用。
编辑应注意,上面的查询包括您的PHP变量。
UPDATE
您在这里有几个问题。
首先,您的复数变量(例如listentrys
)实际上不是数组。 因此,执行插入操作时,仅保留了for
循环中的最后一个值。
其次,您不能以期望的方式将数组插入查询中。 您需要循环每个单独的值。
我的建议是使用数组数组,以便您可以跟踪所需的记录数量,然后循环查询语句。
foreach( $Details as $value )
{
$listentry = $value->getElementsByTagName("listentry");
$sitetype = $value->getElementsByTagName("sitetype");
$sitedescription = $value->getElementsByTagName("sitedescription");
$siteosgb36lat = $value->getElementsByTagName("siteosgb36lat");
$siteosgb36lon = $value->getElementsByTagName("siteosgb36lon");
$new_rows[] = array(
'listentry' => $listentry->item(0)->nodeValue,
'sitetype' => $sitetype->item(0)->nodeValue,
'sitedescription' => $sitedescription->item(0)->nodeValue,
'siteosgb36lat' => $siteosgb36lat->item(0)->nodeValue,
'siteosgb36lon' => $siteosgb36lon->item(0)->nodeValue
);
}
//now loop your query
foreach ($new_rows as $row)
{
$listentry = $row['listentry'];
$sitetype = $row['sitetype'];
$sitedescription = $row['sitedescription'];
$siteosgb36lat = $row['siteosgb36lat'];
$siteosgb36lon = $row['siteosgb36lon'];
//Your query here
}
试试这个代码
mysql_query("INSERT INTO sitestable(listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon) VALUES('".$listentry."','".$sitetype."','".$sitedescription."','".$siteosgb36lat."','".$siteosgb36lon."') ")
or die(mysql_error());
您是否签出了合并声明? 在Msdn上合并
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.