PHP PDO更改表状态以禁止用户

时间:2018-12-18 21:13:55

标签: php pdo

在查看其他人的代码使我的软件可以正常工作的登录系统后,我已经成功完成了工作,我不太熟练使用PHP或PDO,因此,非常感谢我为以下问题提供的帮助。

这是整个代码:

if ( isset($_GET['username'], $_GET['password'], $_GET['hwid']) ) {

//We use prepared statements, why? Because it's much more safe.
$userStatement = $connect->prepare('SELECT * FROM ROE WHERE username = :u AND password = :p');
//We bind each param, no need to use htmlspecialchars or real_escape_string since PDO will do this for us
$userStatement->bindParam(':u', $_GET['username'], PDO::PARAM_STR);
$userStatement->bindParam(':p', $_GET['password'], PDO::PARAM_STR);
//We only continue when the statement is succesfully executed.
if ( $userStatement->execute() ) {
    echo $userStatement->fetchColumn(0) > 0 ? '5352978916' : '0017577757';
}

//We use prepared statements, why? Because it's much more safe.
$userStatement1 = $connect->prepare('SELECT * FROM ROE WHERE hwid = :h');
//We bind each param, no need to use htmlspecialchars or real_escape_string since PDO will do this for us
$userStatement1->bindParam(':h', $_GET['hwid'], PDO::PARAM_STR);
//We only continue when the statement is succesfully executed.
if ( $userStatement1->execute() ) {
    echo $userStatement1->fetchColumn(0) > 0 ? '<br /><br />4201426523' : '<br /><br />8940042580' . $userStatement = $connect->prepare("UPDATE ROE SET status='2' WHERE username=':u'");
}

}

这行代码告诉我用户是否正确登录:

        echo $userStatement1->fetchColumn(0) > 0 ? '<br /><br />4201426523' : '<br /><br />8940042580' . $userStatement = $connect->prepare("UPDATE ROE SET status='2' WHERE username=':u'");

当网站显示4201426523时,此字段正确,而当网站显示8940042580时,此字段不正确。现在,我尝试添加一个禁止功能,如果网站显示8940042580,该功能将起作用,它应该将我的phpmyadmin数据库中的用户“状态”更改为2,但是它不起作用。

有人可以帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

实际上,您仅在最后一个if语句中准备查询:

$userStatement = $connect->prepare("UPDATE ROE SET status='2' WHERE username=':u'"); 

无需执行

$userStatement->execute();

答案 1 :(得分:1)

此声明没有任何意义:

echo $userStatement1->fetchColumn(0) > 0 ? 
    '<br /><br />4201426523' : 
    '<br /><br />8940042580' . $userStatement = $connect->prepare("UPDATE ROE SET status='2' WHERE username=:u");

fetchColumn(0)的结果为0时,您尝试从'<br /><br />8940042580'语句输出连接到结果对象的字符串prepare,该语句应为您提供一个“对象可以无法转换为字符串”错误。

我想你想写的是:

if ($userStatement1->fetchColumn(0) > 0) {
    echo '<br /><br />4201426523';
}
else {
    echo '<br /><br />8940042580';
    $userStatement = $connect->prepare("UPDATE ROE SET status='2' WHERE username=:u");
    $userStatement->bindParam(':u', $_GET['username'], PDO::PARAM_STR);
    $userStatement->execute();
}