Page 1 of 1

duplicate entry problem

Posted: Tue Mar 22, 2005 2:17 pm
by Hammer136
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

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;

}
?>
If you need any further information please ask and i will provide.

Thanks for any help

Hammer

Posted: Tue Mar 22, 2005 2:21 pm
by feyd

Code: Select all

$insert = mysql_query(&quote;insert into $table values ('NULL', '&quote;.$_POST&#1111;'username'].&quote;', '&quote;.$POST&#1111;'email'].&quote;', 'NULL')&quote;)
//                                                                                     ^^^

Posted: Tue Mar 22, 2005 2:32 pm
by Hammer136
Sorry i dnt get wat your trying to show me. I want it to post email and user but what i dont understand is that the email wont be posted even though it is different fron the first entry.

Posted: Tue Mar 22, 2005 2:48 pm
by Burrito
$[underscore]POST

Posted: Tue Mar 22, 2005 2:58 pm
by Hammer136
Oh typical. Totally missed that!

Thanks

Hammer