PDA

Click to See Complete Forum and Search --> : [RESOLVED] user registration?


Justa Lol
Dec 4th, 2009, 12:29 PM
i'm trying to write my website from beginning again because i want to learn programming websites so i decided to delete my old website and start with opening notepad :D well now i come to user registration, i realize that if i just write to the database then it will add the user even if there already is a user with that name... so i come here to ask how i can check if the username is already taken...

menre
Dec 4th, 2009, 12:59 PM
Hi,
I still don't get it how this problem relates to your use of Notepad.

well now i come to user registration, i realize that if i just write to the database then it will add the user even if there already is a user with that name... so i come here to ask how i can check if the username is already taken...
Is this the problem at hand?

Justa Lol
Dec 4th, 2009, 01:31 PM
what i mean is that i make everything from beginning the problem is i need to check if the data is already stored (username) in the database..

edit: i think what you don't get is that i post from a php form into the database...

kows
Dec 4th, 2009, 03:50 PM
your terminology is a little off -- you are not posting into a database. a form in your PHP script is POSTing (the request method) to another PHP script (or perhaps the same script sending the request), which is in turn interacting with your database.

either way, what you need to do is query the database for any users that exist in your database with the username that is currently trying to register. a small example:

<?php
//has the form been submitted?
if($_SERVER['REQUEST_METHOD'] == "POST"){
//yes!

//sanitise the username
$username = mysql_real_escape_string($_POST['username']);

//query the database
$sql = "SELECT uid FROM user_table WHERE username='$username'";
$query = mysql_query($sql);

//if any results returned, this username is in use
if(mysql_num_rows($query) == 0){
//continue with adding the user

//...

}else{
//show some sort of error or something
}
}
?>

ask questions if this doesn't make sense to you.

Justa Lol
Dec 5th, 2009, 05:37 AM
i take this back, i forgot to change uid to id..

thanks kows i got it working :)