XML 和 PHP - 使用表单删除记录 - 致命错误


XML and PHP - Delete record with a form - Fatal Error

我在使用 PHP 从 XML 文件中删除记录时遇到问题。

我不断收到以下错误:

致命错误:无法在第 19 行的/Applications/XAMPP/xamppfiles/htdocs/catalog/deleteaction.php 中使用 DOMElement 类型的对象作为数组

这是代码:

.XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="catalogue_overview.xsl"?>
<catalogue>
  <record>
    <catId>001</catId>
    <title>Fungus</title>
    <location>GP</location>
    <photographer>jrm</photographer>
    <equipment>Canon EOS 40D</equipment>
    <caption>Small fungus</caption>
    <notes>This is a very rare species of fungus</notes>
    <date>10/8/2012</date>
    <imageUrl>images/IMG_1684.jpg</imageUrl>
  </record>
</catalogue>

.PHP目录删除.php

!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Photo Catalogue - Delete Entry</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>

    <h1>Delete Entry From Catalogue</h1>
    <p>DDDDDDD</p>
<?
echo "<form action='deleteaction.php' method='post'>'n";
$doc = new DOMDocument();
$doc->load('catalogue.xml');
$catdelete = $doc->getElementsByTagName("record");
foreach($catdelete as $record) {
  $catIds = $record->getElementsByTagName( "catId" );
  $catId = $catIds->item(0)->nodeValue;
  $titles = $record->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  $locations = $record->getElementsByTagName( "location" );
  $location = $locations->item(0)->nodeValue;
  $photographers = $record->getElementsByTagName( "photographer" );
  $photographer = $photographers->item(0)->nodeValue;
  $equip = $record->getElementsByTagName( "equipment" );
  $equipment = $equip->item(0)->nodeValue;
  $captions = $record->getElementsByTagName( "caption" );
  $caption = $captions->item(0)->nodeValue;
  $note = $record->getElementsByTagName( "notes" );
  $notes = $note->item(0)->nodeValue;
  $dates = $record->getElementsByTagName( "date" );
  $date = $dates->item(0)->nodeValue;
  echo "<input type='checkbox' name='EntriesToRemove[]' value='" . $catId . "'> $title, &quot;$location&quot;<br>'n";
}
echo "<br><input type='submit' value='Delete Entry' />'n</form>'n";
?>
</body>
</html>

删除操作.php

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<?
$entries_to_remove = $_POST['EntriesToRemove'];

$doc = new DOMDocument();
$doc->load("catalogue.xml");
$catalogue = $doc->getElementsByTagName("catalogue");
foreach($catalogue as $record) {
    if($record['catId'] == $_POST["EntriesToRemove"]) {
        // unset($record);
        $catalogue->removeChild($record);
    }
}
$doc->save("catalogue.xml");
?>
</body>
</html>

错误在线:

if($record['catId'] == $_POST["EntriesToRemove"]) {

我敢肯定,我错过了一些简单的问题

任何建议将不胜感激

非常感谢

您已经将EntriesToRemove设置为HTML中的数组 - 因此您需要检查catId是否是该数组的元素而不是等于它(如您链接的答案中所建议的那样)。但是你也需要正确地从 DOMDocument 中获取 catId 值(它不是一个数组) - 你实际上可以使用你在 HTML 中使用的代码来做到这一点,如下所示:

foreach($catalogue as $record) {
    // Get the CatIds from this record
    $catIds = $record->getElementsByTagName( "catId" );
    // get the first CatId (you might actually want to check this exists
    // first if you can't be sure the XML contains that key)
    $catId = $catIds->item(0)->nodeValue;
    // Look to see if the given catId is in the $_POST array
    if (in_array($catId, $_POST['EntriesToRemove'])) {
        // Found it!
    }
}

。应该做这个伎俩。

编辑 我们循环浏览<record>条目的方式也存在问题。而且,由于在执行 removeChild 时修改 DOMDocument 的方式,您无法在foreach循环中删除(请参阅此处的评论:DOMNode::removeChild)。相反,您要做的是存储要删除的节点的副本,然后将其删除到循环之外。

所以,我们最终得到的是:

$catalogue = $doc->getElementsByTagName("catalogue");
$records= $doc->getElementsByTagName('record');
$nodeToRemove= null;
foreach($records as $record) {
    // Get the CatIds from this record
    $catIds = $record->getElementsByTagName( "catId" );
    // get the first CatId (you might actually want to check this exists
    // first if you can't be sure the XML contains that key)
    $catId = $catIds->item(0)->nodeValue;
    // Look to see if the given catId is in the $_POST array
    if (in_array($catId, $_POST['EntriesToRemove'])) {
        // Found it! Store it for removal
        $nodeToRemove= $record;
    }
}
if ($nodeToRemove!=null) {
    $oldnode= $nodeToRemove->parentNode->removeChild($nodeToRemove);
}
$doc->save('catalogue.xml');