How to avoid same userName in database?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to avoid same userName in database?

    Hi guys how do i avoid same user name being used more than once in a database? like if my database already has a user name s3nzo it must reject other registration with the same name or same email here is my code
    PHP Code:
    <?php
        
    require    'inc/head.inc';
        
            
    $loginName    =    mysql_real_escape_string(htmlentities(stripslashes(trim($_POST['loginName']))));
            
    $email        =    mysql_real_escape_string(htmlentities(stripslashes(trim($_POST['email']))));
            
    $pass        =    mysql_real_escape_string(trim(htmlentities(stripslashes(trim(md5($_POST['pass']))))));
            
    $pas2        =    mysql_real_escape_string(trim(htmlentities(stripslashes(trim(md5($_POST['pas2']))))));
            
    $_SESSION['loginName']    =    $loginName;
            
    $_SESSION['email']        =    $email;
            
    $_SESSION['pass']        =    $pass;
        
    $sqle    =    "SELECT * FROM users";
        
    $querye    =    @mysql_query($sqle$con);

                
    $errors    =    array();
                    
    $fetch    =    @mysql_fetch_array($querye);
                        
                            if(
    $_SESSION['loginName'] == $fetch['loginName'])
                                
    $errors    =    'Login name already taken';
                            if(
    $_SESSION['email']        == $fetch['email'])
                                
    $errors    =    'Email address already in use';
                        
                    if(empty(
    $loginName))
                        
    $errors    =    'Login name can not be empty';
                    if(empty(
    $email))
                        
    $errors    =    'Email can not be empty';
                    if(empty(
    $pass) || empty($pas2))
                        
    $errors    =    'Both passwords are required';
                    if(
    $pass != $pas2)
                        
    $errors    =    'Passwords must be the same';
                        
                    if(
    count($errors))
                        {
                            
    $_SESSION['errors']    =    $errors;

                            
    header('Location:register.php');
                        }
                    else
                        {
                            
    $sql    =    "INSERT INTO users    SET    loginName    =    '{$_SESSION['loginName']}',
                                                                email        =    '
    {$_SESSION['email']}',
                                                                password    =    '
    {$_SESSION['pass']}',
                                                                id            =    '{NULL}',
                                                                nick        =    '
    {$_SESSION['loginName']}'";
                            if(!
    $query    =    @mysql_query($sql$con))
                                
    $forms->showError();
                            else
                            {
                                
    $sql2    =    "SELECT * FROM users WHERE loginName    =    '{$_SESSION['loginName']}'";
                                if(!
    $query2    =    @mysql_query($sql2$con))
                                    
    $forms->showError();
                                else
                                    {
                                        if(!
    $rows    =    @mysql_fetch_array($query2))
                                            
    $forms->showError();
                                            else
                                                {
                                                    
                                                    
    $_SESSION['id']            =    $rows['id'];
                                                    
    $_SESSION['nick']                =    $rows['nick'];
                                                    
    $_SESSION['fname']        =    $rows['fname'];
                                                    
    $_SESSION['sname']        =    $rows['sname'];
                                                    
    $_SESSION['perms']        =    $rows['permissions'];
                                                    
    header('Location:home.php');
                                                }
                                    }
                            }
                        }
                    
        require    
    'inc/foot.inc';
    ?>
    it rejects the registration of the same user name only once if you try again it inserts the details into database
    ################################################## #######################

    NEVER mind guys problem solved
    Last edited by s3nzo; 05.09.11, 11:20. Reason: Problem solved
    libra.wen.ru

    #2
    eaiest way to prevent this from happening is to alter your sql:
    PHP Code:
    ALTER TABLE users
    ADD UNIQUE 
    (loginName,email); 

    Comment


      #3
      Then u myt in addition to what he said set database charset to UTF8. That way, sënzø and senzo are the same n cant be registered.

      Comment

      Working...
      X