使用 php 和 mongodb 检查数据库中是否存在用户输入

Check if user input exist in database using php and mongodb

我想先检查电子邮件是否存在,然后再检查密码,但我只想使用 PHP 和 MongoDB 而不是任何其他语言或工具,因为我只是一个初学者。

当我输入内容时,页面变为空白页面,但没有任何反应。我知道我的错误是从数组的行 $findemail 向下。

我是否以正确的方式使用 $cursor 如果是的话,我是否以正确的方式访问 $findemail 使用 $d['email'] 将其与 $email[=17= 进行比较]

<?php
//Include libraries
require __DIR__ . '/vendor/autoload.php';
    
//Create instance of MongoDB client
$mongoClient = (new MongoDB\Client);

//Select a database
$db = $mongoClient->EcommerceWeb;

//Select a collection 
$collection = $db->Employees_Data;

//Extract the data that was sent to the server
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);



$findemail = [
"email" => $email,
"password" => $password
 ];
 

$cursor = $db->$collection->find($findemail);

foreach ($cursor as $d ){

if($d['email'] == $email and $d['passwprd'] == $password){   
    echo "success";
 }
else {
    echo "fail";
}
    }

试试这个:

$cursor = $collection->findOne($findemail);

if($cursor){
    if($cursor->email == $email and $cursor->password == $password){   
        echo "success";
    }
}