簡體   English   中英

插入和更新MySQL表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM