简体   繁体   English

插入和更新MySQL表

[英]Insert & Update MySQL Table

I'm trying to put some code together that inserts data, whilst checking whether the data already exists into a mySQL table. 我正在尝试将一些插入数据的代码放在一起,同时检查数据是否已存在于mySQL表中。

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. 数据源自一个XML文件,我设法将其提取并放入PHP格式,但是我正努力使“插入”和“检查”功能正常工作。

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. 我希望查询检查“ listentry”列中是否有一个条目与我要添加的文件中的数据匹配,如果存在,那么我希望它检查该记录的所有字段并相应地更新到我的表格中

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!"; 

?>

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

As mentioned earlier INSERT .. ON DUPLICATE KEY UPDATE is your best bet. 如前所述, INSERT .. ON DUPLICATE KEY UPDATE是最好的选择。 For your particular query, it would look like this: 对于您的特定查询,它看起来像这样:

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);

This assumes you want to use all the new values, which is what it sounds like and that one of your new values is used in a UNIQUE column. 假设您要使用所有新值,这听起来很像,并且其中一个新值在 UNIQUE 列中使用。

EDIT It should be noted that the query above includes your PHP variables. 编辑应注意,上面的查询包括您的PHP变量。

UPDATE UPDATE

You have a couple of problems here. 您在这里有几个问题。

First, your plural variables (like listentrys ) are not actually arrays. 首先,您的复数变量(例如listentrys )实际上不是数组。 Because of this, when you do your insert, you are only keeping the last value from the for loop. 因此,执行插入操作时,仅保留了for循环中的最后一个值。

Secondly, you can't insert an array into a query in the manner you are expecting. 其次,您不能以期望的方式将数组插入查询中。 You need to loop each individual value. 您需要循环每个单独的值。

My suggestion would be to use an array of arrays so that you can keep track of how many records you want and then loop your query statement. 我的建议是使用数组数组,以便您可以跟踪所需的记录数量,然后循环查询语句。

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
}

try this code 试试这个代码

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

Have you checked out the Merge statement? 您是否签出了合并声明? Merge on Msdn 在Msdn上合并

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM