I am new to XPATH and PHP. I am trying to achieve a user validation with the help of XPATH in PHP. My data is in xml file
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<id>1</id>
<fName>sa</fName>
<lastName>aa</lastName>
<email>[email protected]</email>
<password>a</password>
</customer>
<customer>
<id>2</id>
<fName>bb</fName>
<lastName>cc</lastName>
<email>[email protected]</email>
<password>b</password>
</customer>
</customers>
My php code snippet is
if(file_exists('customer.xml'))
{
$doc = new DOMDocument();
$doc->load('customer.xml');
$xpathvar = new Domxpath($doc);
//check if user exists and password matches
$queryResult = $xpathvar->query("customers/customer[email= '".$userEmail."' and password= '".$password."']");
var_dump($queryResult);
if(count($queryResult)== 1)
{
//successful login
echo "great";
}
else
{
echo "Invalid email address or password";
}
}
No matter what input I provide, count($queryResult) returns 1.
Not sure why the correct matching is not done.
When I provide $userEmail="[email protected]" and the $password="a" the var_dump($queryResult); gives the following output
object(DOMNodeList)[3]
public 'length' => int 0