我想知道是否以及如何检查用户名是否正在使用。

我听说您可以使用jQuery完成此操作,但由于我是初学者,所以我只想简单一些。

我已经尝试过了,但我无法缝制正确。我只是将它连接到mysql数据库,但是由于当一个具有与另一个帐户相同密码的用户名试图登录时,出现了问题,因此我需要这样做来阻止人们添加多个用户名。

这是我注册表格和php的简单代码



<form action="" method="POST">

    <p><label>name&nbsp;&nbsp; : </label>
	 <input id="password" type="text" name="name" placeholder="name" /></p>

    <p><label>User Name : </label>
	<input id="username" type="text" name="username" placeholder="username" /></p>

	<p><label>E-Mail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </label>
	 <input id="password" type="email" name="email"/></p>

     <p><label>Password&nbsp;&nbsp; : </label>
	 <input id="password" type="password" name="password" placeholder="password" /></p>



    <a class="btn" href="login.php">Login</a>
    <input class="btn register" type="submit" name="submit" value="Register" />
    </form>
</div>
<?php

	require('connect.php');
    // If the values are posted, insert them into the database.
    if (isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
		$email = $_POST['email'];
        $password = $_POST['password'];
        $name = $_POST['name'];

        $query = "INSERT INTO `user` (username, password, email, name) VALUES ('$username', '$password', '$email', '$name')";
        $result = mysql_query($query);
        if($result){

        }
    }

?>

最佳答案

对于初学者,您不希望仅依赖于诸如唯一字段之类的东西来进行此操作,当然,您将需要将该属性添加到数据库内的用户名列中,但在此之上,您希望在其上方具有某种正面保护,并且不依赖于数据库在INSERT INTO上抛出错误,您还将想要针对所有这些使用mysqli,而不是旧版本mysql。它很容易被利用,并且不再常见,这是每个文件的外观:


  connect.php


<?php
$conn = mysqli_connect("localhost","username","password","database");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>



  register.php


<form action="insertuser.php" method="POST">
    Username:
    <input type="text" name="username" placeholder="Username" />
    <br />
    Password:
    <input type="password" name="password" placeholder="Password" />
    <br />
    Name:
    <input type="text" name="name" placeholder="Name" />
    <br />
    Email address:
    <input type="text" name="email" placeholder="Email address" />
    <br /><br />
    <input type="submit" value="Register" />
</form>
<?php
// If there's an error
if (isset($_GET['error'])) {
    $error = $_GET['error'];
    if ($error == "usernametaken") {
        echo "<h4>That username is taken!</h4>";
    } elseif ($error == "inserterror") {
        echo "<h4>Some kind of error occured with the insert!</h4>";
    } else {
        echo "<h4>An error occured!</h4>";
    }
    echo "<br />";
}
 ?>
Already have an account? <a href="login.php">Login here</a>



  insertuser.php


<?php
    // Stop header errors
    ob_start();
    // Check if form has been posted
    if (isset($_POST['username'])){
        // Requre database connection file
        require('connect.php');
        // Clean the variables preventing SQL Injection attack
        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $email = mysqli_real_escape_string($conn, $_POST['email']);
        $password = mysqli_real_escape_string($conn, $_POST['password']);
        $name = mysqli_real_escape_string($conn, $_POST['name']);
        // Check if the username exists
        // Construct SELECT query to do this
        $sql = "SELECT id FROM user WHERE username = '".$username."';";
        $result = mysqli_query($conn, $sql);
        $rowsreturned = mysqli_num_rows($result);
        // If the username already exists
        if ($rowsreturned != 0) {
            echo "Username exists, redirecting to register.php with an error GET variable!";
            // Redirect user
            header('Location: register.php?error=usernametaken');
        } else {
            // Construct the INSERT INTO query
            $sql = "INSERT INTO user (username, password, email, name) VALUES ('".$username."', '".$password."', '".$email."', '".$username."');";
            $result = mysqli_query($conn, $sql);
            if($result){
                // User was inserted
                echo "User inserted!";
                /* DO WHATEVER YOU WANT TO DO HERE */
            } else {
                // There was an error inserting
                echo "Error inserting, redirecting to register.php with an error GET variable!";
                // Redirect user
                header('Location: register.php?error=inserterror');
            }
        }
    }
?>


祝您工作顺利,希望对您有所帮助!

詹姆士

关于php - 检查是否使用了用户名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35825308/

10-13 09:49