How to prevent duplicate Usernames in php/mysql

后端 未结 4 1509
滥情空心
滥情空心 2021-01-17 04:47

(This is my first try with php)

I have a very basic register page. http://graves-incorporated.com/test_sites/member_test/register/register.php

* I j

相关标签:
4条回答
  • 2021-01-17 05:25

    From a user interface stand point, I think it would be great if they have the ability to check via ajax request if the username exists in the db or not, sort of like an availability checker.

    It would also save you cpu time if you isolate the process of checking username availability rather than processing everything just to find out that the username is not available.

    To do so via jquery, i would recommend:

    $('#checkAvailabilityButton').click(function() {
        var usernameVal = $('#usernameField').val(); // assuming this is a input text field
        $.post('checkusername.php', {username=usernameVal}, function(data) {
            alert('data');
        });
    });
    

    And on your php end run a query on your database that would look like:

    "SELECT Username FROM users WHERE Username = 'POSTVALUE'"
    
    if (mysql_num_rows > 0) {
        echo "Username is taken"
    }
    

    Also be very very careful not to allow unsanitized post variables into your database to prevent SQL injections.

    Lastly, try to use a better PHP database extensioin like MySQLi, most STMT requests automatically sanitize variables via mysqli_prepare.

    Good luck!

    0 讨论(0)
  • 2021-01-17 05:33

    Which DB driver are you using? Are you looking for DB errors? Some of them just happily sail past any DB errors and you have to call a function specifically to check for DB errors. your DB should throw an error on duplicate data, and you can pick that error up and alert the user.

    0 讨论(0)
  • 2021-01-17 05:36

    You can use ajax query before submit to check/prevent is already exists Also in back-end after you do INSERT INTO... you can do SELECT FOUND_ROWS() to check if it's actually equals 1, it means one row inserted. Then redirect user to error page with information what is not correct. For better user experience, I recommend first option, with JQuery it will not take a lot of time to implement.

    0 讨论(0)
  • 2021-01-17 05:50
    <?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>';
                }
            }
        }
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题