Page 1 of 1

PREVENTING DUPLICATE USERNAME

Posted: Wed Jan 29, 2003 8:46 am
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>

Posted: Wed Jan 29, 2003 9:25 am
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

Posted: Thu Jan 30, 2003 7:08 am
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!