我试图使用MongoDB和PHP创建一个项目。 我创建了一个表并插入了所有数据。 现在,我想使用删除按钮删除记录,并在每行中插入删除链接。 我将其链接到包含以下代码的delete.php。
<?php
require 'vendor/autoload.php';
$id = $_GET['id'];
$conn=new MongoDB\Client("mongodb://localhost:27017");
$db=$conn->project;
$collection=$db->notes;
try{
$id =new MongoDB\BSON\ObjectID($id);
$collection->deleteOne(array('_id'=>$id ));
header('location:real.php');
}
catch(Expection $e){}
?>
但是现在我遇到了这个错误
Uncaught MongoDB\Driver\Exception\InvalidArgumentException: Error parsing ObjectId string: in C:\xampp\htdocs\mongo\delete.php:9 Stack trace: #0 C:\xampp\htdocs\mongo\delete.php(9): MongoDB\BSON\ObjectId->__construct('') #1 {main} thrown in C:\xampp\htdocs\mongo\delete.php on line 9
我的index.php包含以下代码
<div method="post">
<table style="width:50%" border="solid">
<tr>
<th>_id</th>
<th>Author</th>
<th>Subject</th>
<th>Title</th>
<th>Notes</th>
<th>Difficulty</th>
<th>Related Topics</th>
<th>Action</th>
</tr>
<?php
$collection = $db->notes;
$data = $collection->find();
foreach($data as $document){
?>
<tr>
<td><?php echo $document['_id'] ?></td>
<td><?php echo $document['author'] ?></td>
<td><?php echo $document["subject"] ?></td>
<td><?php echo $document["title"] ?></td>
<td><?php echo $document["notes"] ?></td>
<td><?php echo $document["difficulty"] ?></td>
<td><?php echo $document["rel_topic"] ?></td>
<td><a href='delete.php?id=<?php $document['_id'];?>'>Delete</a></td>
</tr>
</table>
</div>
请帮助我!!我是PHP新手