duplicate entry problem

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
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

duplicate entry problem

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;)
//                                                                                     ^^^
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

$[underscore]POST
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Post by Hammer136 »

Oh typical. Totally missed that!

Thanks

Hammer
Post Reply