Home > other >  check if user input exist in database using php and mongodb
check if user input exist in database using php and mongodb

Time:02-10

I want to check first if the email exists then check the password but I only want to use PHP and MongoDB not any other language or tool because I'm just a beginner.

when I enter an input the page change to an empty page and nothing happen. I know my error is from the line of the array $findemail and downward.

am I using the $cursor in the right way and if so am I accessing the $findemail in the right way using $d['email'] to compare it with $email

<?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";
}
    }

CodePudding user response:

Try to this:

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

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