-2

我试图从命令行进行身份验证,但不能很好地工作。请检查我的代码,让我知道我哪里出错了。

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

}

?>
4

1 回答 1

0

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

$username = trim(fgets(STDIN));

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

于 2013-05-02T03:45:47.940 回答