<?php // 定义XML文件路径和记事本数据表名 $xmlFilePath = 'data.xml'; $tableName = 'notes'; // 创建数据库连接(XML文件) $xml = simplexml_load_file($xmlFilePath); // 添加记事本记录 if (isset($_POST['submit'])) { $title = $_POST['title']; $content = $_POST['content']; // 在XML文件中创建新的记事本记录 $newNote = $xml->addChild('note'); $newNote->addChild('title', $title); $newNote->addChild('content', $content); // 保存XML文件 $xml->asXML($xmlFilePath); } // 删除记事本记录 if (isset($_GET['delete'])) { $noteId = $_GET['delete']; // 在XML文件中删除指定记事本记录 foreach ($xml->xpath("//note[@id='$noteId']") as $note) { $noteParent = $note->xpath('..'); unset($noteParent[0][$note->getName()]); } // 保存XML文件 $xml->asXML($xmlFilePath); } /*if (isset($_GET['delete'])) { $noteId = $_GET['delete']; $notes = $xml->xpath("//note[@id='$noteId']"); if (isset($notes[0])) { unset($notes[0]); } $xml->asXML($xmlFilePath); } */ // 更新记事本记录 if (isset($_POST['update'])) { $noteId = $_POST['id']; $title = $_POST['title']; $content = $_POST['content']; // 在XML文件中更新指定记事本记录 foreach ($xml->xpath("//note[@id='$noteId']") as $note) { $note->title = $title; $note->content = $content; } // 保存XML文件 $xml->asXML($xmlFilePath); } // 查询记事本记录 $notes = $xml->xpath("//note"); ?> <!DOCTYPE html> <html> <head> <title>网络记事本</title> </head> <body> <h1>网络记事本</h1> <!-- 添加记事本记录 --> <form method="post" action=""> <label for="title">标题:</label> <input type="text" name="title" id="title" required><br><br> <label for="content">内容:</label><br> <textarea name="content" id="content" rows="4" cols="50" required></textarea><br><br> <input type="submit" name="submit" value="添加记录"> </form> <!-- 显示记事本记录 --> <h2>记事本记录</h2> <ul> <?php foreach ($notes as $note): ?> <li id="note_<?php echo $note->id; ?>"> 标题: <?php echo $note->title; ?>, 内容: <?php echo $note->content; ?> <a href="?delete=<?php echo $note->id; ?>">删除</a> </li> <?php endforeach; ?> </ul> </body> </html>
귀엽고 새로운 것. XML을 이용해 간단한 메모장 기능을 만들어보고 싶어서 코드를 찾았습니다. 실행 후 추가는 가능하지만 삭제할 수는 없습니다. 도와주세요 선생님. 감사합니다~