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

时间:2013-10-23 07:08:29

标签: php html xml xslt

我无法使用PHP从XML文件中删除记录。

我一直收到以下错误:

  

致命错误:不能在第19行的/Applications/XAMPP/xamppfiles/htdocs/catalogue/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 cataloguedelete.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>

deleteaction.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"]) {

我确信这很简单,我错过了

非常感谢任何建议

非常感谢

1 个答案:

答案 0 :(得分:1)

您已将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');