我试图查找用户输入的用户名是否已被其他人使用过,但它似乎无法正常工作。这是代码。我不知道还能放什么。
require 'database.php';
$password = $_POST['password'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];
$message = '';
if ($password === ""){
$message = "<div class='errormsg'>Oops! Must've missed a spot!</div>";
}
else if ($fname === ""){
$message = "<div class='errormsg'>Oops! Must've missed a spot!</div>";
}
else if ($lname === ""){
$message = "<div class='errormsg'>Oops! Must've missed a spot!</div>";
}
else if ($email === ""){
$message = "<div class='errormsg'>Oops! Must've missed a spot!</div>";
}
else if ($username === ""){
$message = "<div class='errormsg'>Oops! Must've missed a spot!</div>";
}
$query = mysql_query("SELECT username FROM users WHERE username='".$username."'");
if (mysql_num_rows($query) != 0)
{
$message = "<div class='errormsg'>Username already exists!</div>";
}
答案 0 :(得分:1)
尝试使用此功能检查具有该名称的现有用户:
function userExists($input) {
$count = mysql_query("SELECT COUNT(*) FROM users WHERE username='".$input."'");
if ($count > 0) $output = "User already exists";
return $output;
}
您可以尝试这样检查:
if (userExists($username)) { ... }
但请考虑将来停止使用mysql_
,因为它已被弃用。