【问题标题】:MongoDB and PHP Error Notice: Undefined index: variableMongoDB 和 PHP 错误提示:未定义索引:变量
【发布时间】: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


【解决方案1】:

在 if 条件之前将 docs 变量初始化为顶部的空数组 -

$docs = [];

然后,在你的 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>

【讨论】:

    【解决方案2】:

    您的输入必须具有“名称”属性。

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

    为了更好的服务器端实践,您应该在使用数组值之前检查键是否存在。就像你对更新密钥所做的那样。

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

    两种方式:

    在一次 isset 调用中检查所有键:

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

    在缺少键时进行循环并抛出异常:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2011-02-21
    • 2016-08-17
    相关资源
    最近更新 更多