我已經建立了一個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爲什麼 –