I'm trying to do a php to update an user account, that I send from android:
$entityBody = file_get_contents('php://input');
$body = json_decode($entityBody,true);
foreach ($body as $value)
{
$username = $value['username'];
$oldPassword = $value['oldPassword'];
$newPassword = $value['newPassword'];
}
I have 3 variables, the username of the account that I want to modify the password, the old password, and the new password.
I do the connection with my database.
$con = new MongoDB\Client;
if($con){
$db = $con->users;
// Select Collection
$collection = $db->user;
Here, I make a filter to search the document that I want.
$filter = array(['Username' => $username,
'Password' => $oldPassword
]);
$qry = new MongoDB\Driver\Query($filter);
I am using the username to find the user in the MongoBD.
$rows = $collection->findOne($qry);
When I've found it, I use and if to see if the username and the password are correct.If it's, I'm trying to change the old password and put the new one,but it's not working.Putting var_dumps, I realized the mistake is in the update line, but I don't know what I am doing wrong in it.
if($rows['Username']== $username && $rows['Password'] == $oldPassword)
{
$criteria = ['Username' => $rows['Username']];
$newData = ['$set' => ['Password' => $newPassword]];
$collection -> update($criteria,$newData);
$changed = true;
echo json_encode(array('status'=> '1','isChanged' => $changed));
}
else{
$changed = false;
echo json_encode(array('status'=> '2','isChanged' => $changed));
}
}
Finally, if the $con is false, I show an error message.
else
{
die("Mongo DB not connected!");
}