找不到检查用户是否已存在的方法

时间:2014-11-02 21:41:03

标签: php mysql database

我使用以下代码检查用户是否已经存在于MySQL数据库中,但每次我尝试使用已经选择的用户名创建新帐户时,我都可以创建它而不是返回错误消息。

<?php
if(isset($_POST['B1'])){
    $user=$_POST['U']; 
    $email=$_POST['email']; 
    $pass=$_POST['P'];
    $cpass=$_POST['CP'];

    if($user!=null && $pass!=null && $cpass!=null && $email!=null){
         if($pass==$cpass){
             $sql="SELECT FROM users (username, email, password) WHERE username='$user'";
             $checkuser=mysql_query($sql); 
             if(mysql_num_rows($checkuser)>0){
                  echo"This username has been already taken.";
             }else{
                 $query="INSERT INTO users(username, email, password) VALUES('$user','$email',md5('$pass'))";
             $result=mysql_query($query) or die("error3");
             mysql_close();
             echo "<center><h2>Thanks for have joined us, ".$user."!</h2><br></center>";
             echo"<center><h2>You are now ready to use your account.</h2><br></center>";
             echo"<center><a href='login.php' class='button specialp'>Let's get started</a></center>";  
             }
        }
    }else{
        echo"<center><p><h2>Oops, something went wrong!<h2></p></center>";
        echo"<center><a href='signup.php' class='button specialp'>Try again</a></center>";
    }

}

?>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

在您的代码部分中

$sql="SELECT username, email, password FROM users  WHERE username='$user'";
$checkuser=mysql_query($sql); 
if(mysql_num_rows() > 0) {
......

尝试替换代码上方的那部分并查看