[英]php check if a username exists in a database
So I have a user form
registration, and what I am trying to do is: while a user is typing an email, the website will check my database if the email already has been used or not, before they hit the register button. 所以我有一个用户
form
注册,我想要做的是:当用户正在键入电子邮件时,如果电子邮件已经被使用,网站将检查我的数据库,然后他们点击注册按钮。
The problem I'm having is that it won't check. 我遇到的问题是它不会检查。 It will only display "Searching in database" .
它只显示“在数据库中搜索” 。 I just want to post my code so maybe someone can catch the error I'm making.
我只是想发布我的代码,所以也许有人可以抓住我正在制作的错误。
This is part of my registration page: 这是我的注册页面的一部分:
<tr class = "spacearound"> <!-- input for email address -->
<th>  Email: </th>
<td>
<input type = "text" id = "user_email" size = "50"
maxlength = "50" name = "u_email"
title = "Enter your email please"
onchange = "EmailCheck();"
onkeypress = "return InputLimiter(event, 'emailCharacters');"
/> *
<span id = "email_status"> </span>
</td>
<td><?php echo $message; ?></td>
</tr>
This is my JavaScript file, "checkusers.js": 这是我的JavaScript文件“checkusers.js”:
$('#user_email').keyup(function() {
var username = $(this).val();
$('#email_status').text('Searching database.');
if(username != ''){
$.post('checkemail.php',{ username: username }, function(data) {
$('#email_status').text(data);
});
} else {
$('#email_status').text('');
}
});
And this is my php file, where I check for an email, "checkemail.php": 这是我的php文件,我查了一封电子邮件,“checkemail.php”:
<?php
define('dbHost', 'xxxxx');
define('dbUser', 'xxxxx');
define('dbPassword', 'xxxxx');
define('dbName', 'xxxxx');
error_reporting(E_ALL ^ E_NOTICE);
$db = mysqli_connect(dbHost, dbUser, dbPassword, dbName);
if(mysqli_connect_errno()) { //if connection database fails
echo("Connection not established ");
} //by now we have connection to the database
if(isset($_POST))['username'])){ //if we get the name succesfully
$username = mysqli_real_escape_string($db, $_POST['username']);
if (!empty($username)) {
$username_query = mysqli_query($db, "SELECT COUNT(`firstName`) FROM `users` WHERE `email`='$username'");
$username_result = mysqli_fetch_row($username_query);
if ($username_result[0] == '0') {
echo 'Email available!';
} else {
echo 'Sorry, the email '.$username.' is taken.';
}
}
}
?>
you have error here 你这里有错误
if(isset($_POST))['username'])){
it should be 它应该是
if(isset($_POST['username'])){
$_POST['username']
should be enclosed inside isset function $_POST['username']
应该包含在isset函数中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.