PREVENTING DUPLICATE USERNAME

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
rkellybis
Forum Newbie
Posts: 3
Joined: Wed Jan 29, 2003 5:26 am
Location: cork

PREVENTING DUPLICATE USERNAME

Post by rkellybis »

I am trying to prevent users from choosing a username that is already in the MYSQL database when they register with the site. This is the code i am trying to use (you can see i am new to this!). It is not giving me any errors but at the same it is not checking that the username already exists. Thanks for the help.

<html>

<body>
<?php

mysql_connect ("localhost", "supaper", "supaper5") or die("Could not Connext");
mysql_select_db("supaper");
?>

<?

$result=mysql_query("Select * from TUTORS where tutor_username = '$tut_uname'");
if(mysql_num_rows($result)>0){
echo ("Username has already been taken!");
}

else


mysql_query("INSERT INTO TUTORS(tutor_username, tutor_pwd, tutor_name, tutor_email, tutor_address, tutor_phone, tutor_course, tutor_startdate)
VALUES ('$tut_uname', '$tut_pwd', '$tut_name', '$tut_email', '$tut_addr', '$tut_phone', '$tut_phone', SYSDATE() )");
$redirect = "../main/Tutor_RegisterSuccess_Page.php";
echo "<script language='Javascript'>\n
document.location.replace('$redirect');
</script>\n";
die();
?>
</body>
</html>
Last edited by rkellybis on Wed Jan 29, 2003 9:42 am, edited 1 time in total.
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Post by DaiWelsh »

Realistically you are going to have to wait until they submit the form before you check whether the username exists. A php script runnng on the server can query the database with something like SELECT * FROM Users WHERE Username = '$new_username' to find out if the username exists, but the javascript validation takes place on the users browser and does not have direct access to the database.

While there are technically ways to include it in the js validation they are not straightforward (hidden frame for example) or not secure (e.g. having list of existing usernames in page) afaik.

HTH,

Dai
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

you seem to be missing your brackets { ......... } after the else statement

instead of using the JavaScript redirects you might want to consider using the PHP header() function to redirect to the page you want to call, remember to remove all the HTML input before the header() call though!
Post Reply