I am trying to create a form where in I will add the data, delete and update using MongoDB and PHP. But now I am stuck with update part. As soon as I click Update button it throws following error and remove all previous existed record data.
This is the update.php file.
<?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>
But now constantly getting this error
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
Please help Me!!!