插入&更新MySQL表

时间:2011-10-20 15:07:41

标签: php mysql

我正在尝试将一些插入数据的代码放在一起,同时检查数据是否已经存在于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!"; 

?>

4 个答案:

答案 0 :(得分:3)

如果listentry属性是表的主键,或者定义了唯一索引,则可以使用INSERT ... ON DUPLICATE KEY UPDATE命令,有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

答案 1 :(得分:2)

如前所述,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变量。

<强>更新

你有几个问题。

首先,您的复数变量(如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
}

答案 2 :(得分:1)

试试这段代码

mysql_query("INSERT INTO sitestable(listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon) VALUES('".$listentry."','".$sitetype."','".$sitedescription."','".$siteosgb36lat."','".$siteosgb36lon."') ") 
or die(mysql_error());

答案 3 :(得分:-1)

您检查过合并声明吗? Merge on Msdn