Another Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

Another Problem

Post by Darksevear »

Im just wondering,

If someone could show me how when a user enters a username the php script checks the database to see if that username has been used before. If it has been user then let the user chose another one, and if it hasn't then continue on to the next webpage.

Please show me.

Thanks, Daniel.

Or can you point me towards a tutorial that can.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Logic:

User submits username.
Check if that username is in the database. mysql_query() (SELECT)
If it is, show them an error prompting them to choose a different username. if(in database){ error }
If it isn't, allow them to continue the script. else { continue }
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

Code

Post by Darksevear »

Can u write me a quick example please.

- Cheers, Daniel
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$db = mysql_connect('localhost','user','password') or die(mysql_error());
mysql_select_db('my_database',$db);

if(!empty($_POST)){

    $username = mysql_real_escape_string($_POST['username']);

    $result = mysql_query("SELECT count(*) FROM `users` WHERE `username` = '$username'") or die(mysql_error());
    $count = mysql_result($result,0);

    if($count > 0){
        die('This username is already in use.  Please select a different username.');
    }

    //    continue script here
    //    the username does not already exist

}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

Post by Darksevear »

Thanks
Post Reply