Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

check if user input exist in database using php and mongodb

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Try to this:

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading