duplicate entry problem
Posted: Tue Mar 22, 2005 2:17 pm
HI
Im trying to create a registry form which has 2 unique keys (username and email) and i have it so that it creates 1 account fine but on the second attempt is says that i have "Duplicate entry " for key 3. I think its about having the email as a unique key but i am unsure about how to sort it.
So far my code looks like this
If you need any further information please ask and i will provide.
Thanks for any help
Hammer
Im trying to create a registry form which has 2 unique keys (username and email) and i have it so that it creates 1 account fine but on the second attempt is says that i have "Duplicate entry " for key 3. I think its about having the email as a unique key but i am unsure about how to sort it.
So far my code looks like this
Code: Select all
<?php
include("config.php");
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';";
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
exit;
} else {
function checkEmail($email) {
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)){
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
return false;
}
return true;
}
return false;
}
$email = trim($_POST['email']);
if(!checkEmail($email)) {
echo 'Invalid email address!<br>';
}
else {
echo 'Email address is valid<br>';
}
// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$POST['email']."', 'NULL')")
or die("Could not insert data because ".mysql_error());
$id = md5(uniqid(rand(),true));
// print a success message
echo "Your user account has been created!<br>";
echo "An email will be sent to you shortly";
echo $id;
}
?>Thanks for any help
Hammer