如何防止php/mysql中重复的用户名

Dan*_*ves 0 php mysql registration duplicates

(这是我第一次尝试使用php)

我有一个非常基本的注册页面. http://graves-incorporated.com/test_sites/member_test/register/register.php

*我只记得PHP代码没有出现在源代码中,所以这里是:

enter code here
<?php
include('connection.php');

if(isset($_POST['form'])){
    if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['conf_pass']) || empty($_POST['email'])){
        echo '<b>Please fill out all fields.</b>';

    }elseif($_POST['password'] != $_POST['conf_pass']){
        echo '<b>Your Passwords do not match.</b>';
    }else{
        $url = 'http://graves-incorporated.com/test_sites/plantation_park_2012/';
        echo '<META HTTP-EQUIV=Refresh CONTENT="2; URL='.$url.'">';
        echo '<b>Congrats, You are now Registered.</b>';
        mysql_query("INSERT INTO users VALUES(NULL, '$_POST[username]', '$_POST[password]', '$_POST[email]')");     
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

我想确保我没有在数据库中获得重复的用户和/或电子邮件地址.我在MySQL中为用户名和电子邮件设置了一个唯一密钥,这可以防止它,但是实际表单上的用户不知道,它仍然告诉他们"恭喜,你已经注册"或者其他什么......哈哈

那么我可以添加到php代码(以及代码中的哪个位置)来阻止这种情况呢?

感谢帮助这个主要的菜鸟,丹格雷夫斯

mgr*_*aph 5

<?php
include('connection.php');

if(isset($_POST['form'])){
    if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['conf_pass']) || empty($_POST['email'])){
        echo '<b>Please fill out all fields.</b>';

    }elseif($_POST['password'] != $_POST['conf_pass']){
        echo '<b>Your Passwords do not match.</b>';
    }else{

        $dup = mysql_query("SELECT username FROM users WHERE username='".$_POST['username']."'");
        if(mysql_num_rows($dup) >0){
            echo '<b>username Already Used.</b>';
        }
        else{
            $url = 'http://graves-incorporated.com/test_sites/plantation_park_2012/';
            echo '<META HTTP-EQUIV=Refresh CONTENT="2; URL='.$url.'">';

            $sql = mysql_query("INSERT INTO users VALUES(NULL, '$_POST[username]', '$_POST[password]', '$_POST[email]')");     
            if($sql){
                 echo '<b>Congrats, You are now Registered.</b>';
            }
            else{
                echo '<b>Error Registeration.</b>';
            }
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)