我已经建立了一个pdo连接,并将其作为变量传递给一个函数。这一切工作正常,该功能正确返回。如果我使用PDO变量和名称在条件语句中运行该函数,它将正确运行 - 如果名称位于数据库中,则它会正确回应,如果不是,则它也会正确回显。我想要做的是将表单帖子的值传递给函数,以便检查它是否存在于数据库中。这里是我的代码:
函数检查列计数是否为1。
function user_exists($pdo, $username) {
$stmt = $pdo->prepare('SELECT COUNT(uid) FROM users WHERE username = :username');
$stmt->execute(['username' => $username]);
$result = $stmt->fetchColumn();
return ($result == 1);
}
如果管理员用户在数据库中存在回声“存在” - 只是用于测试。
if(user_exists($pdo,'admin') == true) {
echo "exists";
} else {
echo "doesnt exist";
}
检查,看看这两个领域已经进入那么我想它来检查输入的用户名在数据库中,但我做错了什么。
if(!empty($_POST) === true) {
$username = $_POST['username'];
$pwood = $_POST['password'];
if(empty($username) === true || empty($pwood) === true) {
echo "You need to enter a username and password";
} else if (user_exists($pdo,$_POST['username']) == false){
echo 'We can\'t find that username. Please try again or register';
}
}
此行'如果(!空($ _ POST)===真){' – Akintunde007
完美非常感谢。我不知道为什么。 post的值不为空...... –
@GrumpyCrouton,使用'global'变量*不推荐。请参阅https://stackoverflow.com/questions/5166087/php-global-in-functions或https://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if -so-why为什么 –