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>
PREVENTING DUPLICATE USERNAME
Moderator: General Moderators
PREVENTING DUPLICATE USERNAME
Last edited by rkellybis on Wed Jan 29, 2003 9:42 am, edited 1 time in total.
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
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