Page 1 of 1

Another Problem

Posted: Sun May 14, 2006 1:46 am
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.

Posted: Sun May 14, 2006 2:03 am
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 }

Code

Posted: Sun May 14, 2006 2:13 am
by Darksevear
Can u write me a quick example please.

- Cheers, Daniel

Posted: Sun May 14, 2006 4:37 am
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

}

Posted: Sun May 14, 2006 5:58 am
by Darksevear
Thanks