www.webdeveloper.com
Results 1 to 4 of 4

Thread: checking to see if a user name already exists

  1. #1
    Join Date
    Nov 2007
    Location
    Parts Unknown......
    Posts
    88

    checking to see if a user name already exists

    Hi, I am working on a login system and I am trying to see if a user name already exist, then the user would have to choose another user name.

    I am not sure how to check the table and see. And yeah, I am a php newbie.

    Thanks in advance!

    Here is my code.

    PHP Code:
    <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    <html>
    <head>
           <title>added</title>
    </head>
    <body>
    <?php

    $dbNewUser 
    $_REQUEST['newuser']; // this gets the user name from the register.html form
    $dbNewPassword $_REQUEST['newpassword'];  // this gets the password from the register.html form

    $dbh=mysql_connect ("localhost""hrpjeff_root""password") or die ('I cannot connect to the database because: ' mysql_error());  // this connects to the database of dies
    mysql_select_db ("hrpjeff_time");

    $query "INSERT INTO users (id, name, password) VALUES ( 0, '$dbNewUser', MD5( '$dbNewPassword' ) )"// this inserts the new user name and password into the table

    $result mysql_query($query) or die("Query failed: " mysql_error()); // if it is not saved then a error is thrown

    ?>
    </body>
    </html>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    Assign a unique index to the `id` column in the users table (or make it the primary key if applicable). Then when you do your query, check for a duplicate key error:
    PHP Code:
    $result mysql_query($query);
    if(!
    $result)
    {
       if(
    mysql_errno() == 1062)  // duplicate entry error
       
    {
          
    // output error message telling user to try another login ID
       
    }
       else
       {
          
    // some other error, so log it and display general error message to users
       
    }
    }
    else
    {
       
    // new user account successfully created

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2007
    Location
    Parts Unknown......
    Posts
    88
    Here is what I have tried so far. I left the errors for testing purposes. I also changed the id to the primary key. I still can create more than one account with the same user name.

    I would think that I need to check this before writing it to the table.

    Thanks for the help so far. Any other suggestions?

    PHP Code:
    <?php
        
    $dbNewUser 
    $_REQUEST['newuser']; // this gets the user name from the register.html form
    $dbNewPassword $_REQUEST['newpassword'];  // this gets the password from the register.html form

    $dbh=mysql_connect ("localhost""hrpjeff_root""password") or die ('I cannot connect to the database because: ' mysql_error());  // this connects to the database of dies
    mysql_select_db ("hrpjeff_time");

    $query "INSERT INTO users (id, name, password) VALUES ( 0, '$dbNewUser', MD5( '$dbNewPassword' ) )"// this inserts the new user name and password into the table

    //$result = mysql_query($query) or die("Query failed: " . mysql_error()); // if it is not saved then a error is thrown

    //header( "Location: index.php" ); // redirect to the login if registration is successful.

    $result mysql_query($query);
    if(!
    $result)
    {
       if(
    mysql_errno() == 1062)  // duplicate entry error
       
    {
         echo 
    " // output error message telling user to try another login ID";
       }
       else
       {
          echo 
    "// some other error, so log it and display general error message to users";
       }
    }
    else
    {
       echo 
    "// new user account successfully created";

        
    ?>

  4. #4
    Join Date
    Nov 2007
    Location
    Parts Unknown......
    Posts
    88
    Thanks for the help. I seem to have figured it out myself!

    Now on to the next problem......

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles