Jump to content

duplicate sql entries


wiggly

Recommended Posts

Hi i am a complete newbe to php and thus i dont know very much however i have started writing a registration/login system for my website and i can not seem to figure out how to stop duplicate usernames or email addresses i have managed to get the actualy registration part working and i just need a bit of advice on how i could stop multiple people using the same details. i did find a tutorial that i tried to use some code from on phpfreaks and i had no luck.
this is the code i tried:

$sql_email_check = mysql_query("SELECT email form users
WHERE email_address='$email");
$sql_username_check = mysql_query("SELECT username from users
WHERE username='$username'");

$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);

if(($mail_check > 0) || ($username_check > 0)){
echo "Please fix the following errors: <br />";
if($email_check > 0){
echo "<strong>Your Email Address has already been used by another member in our database please submit a diffrent Email Address!</strong> <br />";
unset($email);
}
if($username_check > 0){
echo "<strong>The Username you selected has alreadey been used by another member in our database. Please choose a diffrent Username</strong> <br />";
unset($username);
}

i would be most greatfull if someone could help me as i have been bugged for the past week with this problem, maybee i have just typed somthing wrong or missed somthing out i have no idea.
Link to comment
Share on other sites

You don't say what the problem is. Just posting code and saying "It doesn't work" does not give anyone much of clue about what to look for.

I did notice in this line below you have used $mail_check instead of "$email_check"
[code]if(($mail_check > 0) || ($username_check > 0)){[/code]

Also there are a lot of old tutes out there which assume register_globals is set ON whereas with later version of php it is set OFF by default. This means you may have explicitly get the the username and email address from the $_GET or $_POST arrays at the beginning of your script

[code]$username = $_POST['username'];
$email = $_POST['email'];[/code]
Link to comment
Share on other sites

[code]
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Server\Apache2\htdocs\login2\register.php on line 45

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Server\Apache2\htdocs\login2\register.php on line 46
[/code]

these are the errors i get when i use this piece of code however when i take the code out i get no errors what i want to do is add code to my register.php to stop people registering with a username and email address that is already registered.

here is my code with the above problems removed maybee that will be more helpfull to you

[code]
<?

include('include/db_connect.php');

$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$info = $_POST['info'];

$first = stripslashes($first);
$last = stripslashes($last);
$email = stripslashes($email);
$phone = stripslashes($phone);
$user = stripslashes($user);
$pass = stripslashes($pass);
$info = stripslashes($info);

if((!$first) || (!$last) || (!$email) || (!$user) || (!$pass)){
    echo '<strong>You must fill in all required fields!!!</strong> <br />';
    if(!$first){
        echo "You must fill in the First Name field! <br />";
    }
    if(!$last){
        echo "You must fill in the Last Name field! <br />";
    }
    if(!$email){
        echo "You must fill in the E-Mail field! <br />";
    }
    if(!$user){
        echo "You must fill in the Username field! <br />";
    }
    if(!$pass){
        echo "You must fill in the Password field! <br />";
    }
    include 'register.html';
    exit();
}
$pass=md5($pass);

$sql = mysql_query("INSERT INTO users (first, last, email, phone, user, pass, info) VALUES('$first', '$last', '$email', '$phone', '$user', '$pass', '$info')")
    or die (mysql_error());
    
if(!$sql){
    echo 'There has been a problem during the registration process please try again later! <br />';
}
else{
$userid = mysql_insert_id();
    echo 'Your account was created sucsesfully <br />';
}
?>
[/code]
Link to comment
Share on other sites

You get those messages because of errors in the sql queries.

There is certainly an error in the first, FROM is mis-spelt as FORM

[code]$sql_email_check = mysql_query("SELECT email form users
WHERE email_address='$email");
$sql_username_check = mysql_query("SELECT username from users
WHERE username='$username'");[/code]

Use this code to get the error messages

[code]$sql_email_check = mysql_query("SELECT email from users
WHERE email_address='$email") or die (mysql_error() . ' in query ' . $sql_email_check);
$sql_username_check = mysql_query("SELECT username from users
WHERE username='$username'") or die (mysql_error() . ' in query ' . $sql_username_check);[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.