【问题标题】:Insert & Update MySQL Table插入和更新 MySQL 表
【发布时间】:2011-10-20 15:07:40
【问题描述】:

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

?>

【问题讨论】:

    标签: php mysql


    【解决方案1】:

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

    【讨论】:

      【解决方案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
      }
      

      【讨论】:

      • 您好,非常感谢您回复我的帖子。我确实尝试了代码,但不幸的是无法让它运行。然而,在一些进一步的研究中,我认为“插入忽略”就足够了,但我对此有疑问。基本上代码运行,但它只插入最后一条记录。我已经更新了我原来的帖子。我只是想知道你是否可以看看它,让我知道我哪里出错了。亲切的问候
      • 嗨@IRHM,你确定INSERT IGNORE是你想要的吗?如果存在重复键,则不会插入新数据。 There is an SO answer by Bill Karwin 更好地解释了它。我还更新了我的答案以反映您的问题。
      【解决方案3】:

      试试这个代码

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

      【讨论】:

        【解决方案4】:

        您检查过 Merge 语句吗? Merge on Msdn

        【讨论】:

        • 这是关于 mySQL,而不是 T-SQL
        猜你喜欢
        • 1970-01-01
        • 2015-04-25
        • 1970-01-01
        • 2022-10-02
        • 2020-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多