检查我的数据库中已存在的用户名(MySQL)

时间:2014-03-25 07:25:51

标签: php html mysql

我有一个要求用户输入用户名的注册表。

我需要检查他们输入的用户名是否已存在于我的数据库中。但是以下代码无效。

<ul class="user">
<li class="label">User name </li>
<li class="field">
<input type="text" id="username" name="username" class="required error"/>
<?php
    // $username = $_POST['username'];    
    $dbhost = 'localhost:3306';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'cast';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
    if(! $conn )
    {
       die('Could not connect: ' . mysql_error());
    }
    else
    {
       echo "Connect";
    }

    mysql_select_db('cast');
    $query = mysql_query("SELECT username FROM usertable WHERE username='$username'");

    if (mysql_num_rows($query) != 0)
    {
       echo "Username already exists";
    }
?>
</li>
</ul>

2 个答案:

答案 0 :(得分:0)

我建议您使用 MySQLi 并使用 isset 来实现此目的。您可以尝试以下代码:

<html>
<body>

<?php

/* START OF CONNECTION */
$conn = mysqli_connect("localhost","root","","cast");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}

if(isset($_POST['submit'])){ /* IF SUBMIT IS CLICKED */

$username=mysqli_real_escape_string($conn,$_POST['username']); /* TO PREVENT SOME OF SQL INJECTION */
$query = mysqli_query($conn,"SELECT username FROM usertable WHERE username='$username'");

if(mysqli_num_rows($query)==0){ /* IF NO ACCOUNT IS FOUND */
header("LOCATION:proceed.php"); /* IT WILL PROCEED TO A NEW PAGE */
}

else { /* ELSE IF THERE'S A USERNAME FOUND */
echo "Username is already taken. Please try again."; /* IT WILL STAY ON THE SAME PAGE */
}

} /* END OF ISSET SUBMIT */

?>

<form action="" method="POST">
<ul class="user">
<li class="label">User name </li>
<li class="field">
<input type="text" id="username" name="username" class="required error"/></li>
<li><input type="submit" value="Validate" name="submit"></li>
</ul>
</form>

</body>
</html>

答案 1 :(得分:0)

<?php

    if (isset($_POST['username'])) // Its a submission
    {
        $dbhost = 'localhost:3306';
        $dbuser = 'root';
        $dbpass = '';
        $dbname = 'cast';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
        if(!$conn )
        {
            die('Could not connect: ' . mysql_error());
        }

            $username = mysql_escape_string(trim($_POST['username'])); // Sanitize
            if(empty($username)) {
                echo "Please enter an username";
            } else {
                mysql_select_db($dbname);
                $query = mysql_query("SELECT * FROM usertable WHERE name='$username' LIMIT 1");

                while ($row = mysql_fetch_array($query)) {
                    if($row){
                        echo('Username exists');
                    } else {
                        // do further registration
                    }
                }
            }
    }

?>
<form id="usertype" action="<?= htmlentities($_SERVER['PHP_SELF']); ?>" method="post" >
    <center>
        <h1> User Registration Page </h1>
        <div id="content">
            <input type="textbox" id="username" name="username" value="" />
            <input type="submit" name="submit" value="Submit"/>
        </div>
    </center>
</form>