我无法使用 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, "$location"<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"]) {
我确定这很简单,我错过了
任何建议将不胜感激
非常感谢