从命令行错误进行身份验证


Authenticate from command line error

我尝试从命令行进行身份验证,但似乎无法正常工作。请检查我的代码,让我知道我错在哪里。

<?php
echo "enter your username'n";
$username = fgets(STDIN);
echo "your password'n";
$password =  fgets(STDIN);
echo "enter your new password'n";
$newpassword = fgets (STDIN);
//connecting to database
$db = mysql_connect("localhost","sqldata","sqldata") or die(mysql_error());
//selecting our database
$db_select = mysql_select_db("accounts", $db) or die(mysql_error());
$sql = mysql_query ("select * from uptable where username ='$username'");
$result = mysql_fetch_assoc($sql);
$oldpassword = $result['password'];
if ($password != $oldpassword)
{
echo "error";
}
else
{
mysql_query("update uptable set password = '$newpassword' where username ='$username'");
echo "Your Password has been successfully changed";
}
?>

将所有的fgets()赋值改为使用trim(),例如:

$username = trim(fgets(STDIN));

fgets()返回的字符串包含换行符,但数据库中的姓名和密码可能不包含换行符