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

时间:2013-05-02 03:27:21

标签: php mysql

我尝试从命令行进行身份验证,但不能正常工作。 请检查我的代码并告诉我出错的地方。

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

}

?>

1 个答案:

答案 0 :(得分:0)

更改所有fgets()分配以使用trim(),例如:

$username = trim(fgets(STDIN));

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