2017-03-05 34 views
-1

这是我的代码。一切都很完美。它防止用户名重复。我遇到的唯一问题是当已经注册的用户尝试再次注册时回显“用户已经存在”。回声用户已存在

<!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> 
+1

我看不到任何代码检查,如果他们已经存在 – nogad

+1

其他问题:stripslashes上的密码,密码上的md5。 – nogad

+0

所以你需要检查用户名是否已经存在,如果用户尝试使用相同的用户名注册? – Mario

回答

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> 
0

之前将运行像

SELECT * FROM `users` WHERE `username` = '$username' 

查询然后检查用户是否已经存在与否,并采用访问数据库的一些安全的方式,即PDO

-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"; 
}