这是我的代码。一切都很完美。它可以防止用户名重复。我已经遇到的唯一问题是当已经注册的用户再次尝试注册时回显“用户已经存在”。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="style2.css" />
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","","register");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_REQUEST['username'])){
$username = stripslashes($_REQUEST['username']);
$username = mysqli_real_escape_string($con,$username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$query = "INSERT into `users` (username, password)
VALUES ('$username', '".md5($password)."')";
$result = mysqli_query($con,$query);
if($result){
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php } ?>
</body>
</html>
答案 0 :(得分:2)
这应该有效,问题是你在插入另一个之前没有检查用户名是否存在。你会在我的代码中看到我评论它。
不要使用md5加密密码,因为它不安全,请使用password_hash()和password_verify()函数。
<?php
$con = mysqli_connect("localhost", "root", "", "register");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// if is pressed submit button ( Register )
if (isset($_POST['submit']))
{
$username = stripslashes($_POST['username']);
$username = mysqli_real_escape_string($con, $username);
$password = stripslashes($_POST['password']);
$password = mysqli_real_escape_string($con, $password);
// check if username already exists
$q = mysqli_query($con, "SELECT username FROM users WHERE username = '$username'");
$user_exists = mysqli_num_rows($q);
// if number of rows is grateater that 0 it's mean that username exists
if ($user_exists > 0)
{
echo "Username in use, please select another one.";
}
else
{
// if username don't exists
$query = "INSERT INTO users (username, password) VALUES ('$username', '".md5($password)."')";
$result = mysqli_query($con,$query);
if($result)
{
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a>
</div>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="style2.css" />
</head>
<body>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
</body>
</html>
答案 1 :(得分:0)
在插入之前运行像
这样的查询SELECT * FROM `users` WHERE `username` = '$username'
然后检查用户是否已经存在并采用一些安全的访问数据库的方式,即pdo
答案 2 :(得分:-1)
您需要计算表行以检查用户名是否已存在
$count = "SELECT COUNT(username) as count FROM table WHERE username = '$username'";
$result = mysqli_query($count);
$users = mysqli_fetch_assoc($result);
$numrows = $users['username'];
If ($numrows == 1) {
Echo "your message";
}