0

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!!!

TusharD
  • 13
  • 6
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – LF00 Sep 17 '20 at 10:02

2 Answers2

0

Your inputs must have a "name" property.

 Author:<input type='text' name='author' value='<?php echo $docs['author']?>'/><br>

For a better server-side practice, You should check that keys exists before using array values. Like you did for update key.

if(isset($_POST['update'])){

2 ways :

Check all keys in one isset call:

if(isset($_POST['author'], $_POST['subject'])){

Make a loop and throw an exception when a key is missing:

foreach (['author', 'subject'] as $key) {
    if (isset($_POST[$key])) {
        throw new Exception("Key '$key' is missing");
    }
}

landrok
  • 54
  • 5
  • Good to know, can you please accept the answer ? [How to accept an answer](https://stackoverflow.com/help/someone-answers) – landrok Sep 17 '20 at 13:02
0

Initialise docs variable as empty array at the top before if condition -

$docs = [];

Then, Replace this in your html -

<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>
DIGVJSS
  • 463
  • 1
  • 10
  • 21