【发布时间】:2021-01-04 04:52:17
【问题描述】:
我正在尝试创建一个表单,我将在其中使用 MongoDB 和 PHP 添加、删除和更新数据。 但现在我被更新部分困住了。 一旦我单击更新按钮,它就会引发以下错误并删除所有以前存在的记录数据。
这是 update.php 文件。
<?php
require 'vendor/autoload.php';
$conn=new MongoDB\Client("mongodb://localhost:27017");
$db=$conn->project;
$collection=$db->notes;
if(!empty($_GET['id'])){
$id = $_GET['id'];
$id =new MongoDB\BSON\ObjectId($id);
$docs=$collection->findOne(array('_id'=>$id));
?>
<?php
try{
if(isset($_POST['update'])){
$collection->updateOne(
array('_id' => $id),
array(
'$set' => array(
'author' => $_POST['author'],
'subject' => $_POST['subject'],
'title' => $_POST['title'],
'notes' => $_POST['notes'],
'difficulty' => $_POST['difficulty'],
'rel_topic' => $_POST['rel_topic']
)
)
);
echo " Updated successfully";
// header("location:real.php");
}
}
catch(Exception $e){
echo 'Error '.$e->getMessage();
}
}
?>
<form method="post">
<p>ID: <?php echo $docs['_id']?></p>
Author:<input type='text' id='author' value='<?php echo $docs['author']?>'/><br>
Title:<input type='text' id='title' value='<?php echo $docs['title']?>'/><br>
Subject:<input type='text' id='subject' value='<?php echo $docs['subject']?>'/><br>
Notes:<input type='text' id='notes' value='<?php echo $docs['notes']?>'/><br>
Difficulty:<input type='text' id='difficulty' value='<?php echo $docs['difficulty']?>'/><br>
Related Topic:<input type='text' id='rel_topic' value='<?php echo $docs['rel_topic']?>'/><br>
<button type="submit" name="update">Update</button>
</form>
但是现在经常出现这个错误
Notice: Undefined index: author in C:\xampp\htdocs\mongo\update.php on line 21
Notice: Undefined index: subject in C:\xampp\htdocs\mongo\update.php on line 22
Notice: Undefined index: title in C:\xampp\htdocs\mongo\update.php on line 23
Notice: Undefined index: notes in C:\xampp\htdocs\mongo\update.php on line 24
Notice: Undefined index: difficulty in C:\xampp\htdocs\mongo\update.php on line 25
Notice: Undefined index: rel_topic in C:\xampp\htdocs\mongo\update.php on line 26
请帮帮我!!!
【问题讨论】:
标签: php html mongodb mongodb-query xampp