我最近在我的注册系统遇到了一些麻烦,我试图在很多方面防止名称重复,并且已经浏览了stackoverflow。 但我似乎无法找到解决方案。
这是我的代码
<?php
if (isset($_POST['registerBtn']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$rpassword = $_POST['rpassword'];
$email = $_POST['email'];
$errors = array();
$checkUsername = $odb -> prepare("SELECT COUNT(*) as total from FROM `users` WHERE `username` = :username");
$checkUsername -> execute(array(':username' => $username));
$countUsername = $checkUsername -> fetchColumn(0);
if ($checkUsername > 0)
{
$errors['Username is already taken'];
}
if (!ctype_alnum($username) || strlen($username) < 4 || strlen($username) > 15)
{
$errors[] = 'Username Must Be Alphanumberic And 4-15 characters in length';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = 'Email is invalid';
}
if (empty($username) || empty($password) || empty($rpassword) || empty($email))
{
$errors[] = 'Please fill in all fields';
}
if ($password != $rpassword)
{
$errors[] = 'Passwords do not match';
}
if (empty($errors))
{
$insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0)");
$insertUser -> execute(array(':username' => $username, ':password' => SHA1($password), ':email' => $email));
echo '<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>User has been registered. Redirecting....</p></div><meta http-equiv="refresh" content="3;url=login.php">';
}
else
{
echo '<div class="nNote nFailure hideit"><p><strong>ERROR:</strong><br />';
foreach($errors as $error)
{
echo '-'.$error.'<br />';
}
echo '</div>';
}
}
?>
答案 0 :(得分:0)
您可以在用户名上设置UNIQUE
约束:
CREATE TABLE users
(
id int NOT NULL AUTO_INCREMENT,
username varchar(30),
password varchar(80),
email varchar(80),
PRIMARY KEY (id),
UNIQUE KEY username (username)
)
答案 1 :(得分:0)
您应该与$countUsername
进行比较,而不是$checkUsername
。
if ($checkUsername > 0)
更改为
if ($countUsername> 0)
此外,此行应更改为:
if ($checkUsername > 0)
{
$errors['Username is already taken'];
}
要
if ($checkUsername > 0)
{
$errors[] = 'Username is already taken';
}
见下文:
<?php
if (isset($_POST['registerBtn']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$rpassword = $_POST['rpassword'];
$email = $_POST['email'];
$errors = array();
$checkUsername = $odb -> prepare("SELECT COUNT(*) as total from FROM `users` WHERE `username` = :username");
$checkUsername -> execute(array(':username' => $username));
$countUsername = $checkUsername -> fetchColumn(0);
$errors = [];
if ($countUsername > 0)
{
$errors[] = 'Username is already taken';
}
if (!ctype_alnum($username) || strlen($username) < 4 || strlen($username) > 15)
{
$errors[] = 'Username Must Be Alphanumberic And 4-15 characters in length';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = 'Email is invalid';
}
if (empty($username) || empty($password) || empty($rpassword) || empty($email))
{
$errors[] = 'Please fill in all fields';
}
if ($password != $rpassword)
{
$errors[] = 'Passwords do not match';
}
if (count($errors) > 0)
{
$insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0)");
$insertUser -> execute(array(':username' => $username, ':password' => SHA1($password), ':email' => $email));
echo '<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>User has been registered. Redirecting....</p></div><meta http-equiv="refresh" content="3;url=login.php">';
}
else
{
echo '<div class="nNote nFailure hideit"><p><strong>ERROR:</strong><br />';
foreach($errors as $error)
{
echo '-'.$error.'<br />';
}
echo '</div>';
}
}
?>