Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts

    Unhappy Prevent duplicate content in database does NOT work!

    I am having a big problem in MySQL and a part in my PHP coding as well.

    I created a registration form, where the user will have to fill his names, email address, choose a username and so on. I do not want duplicate email address and username in my database, thus, if a user choose either an email address or a username which has already taken, he will be notified.

    To prevent this duplication, I have set both the email address and username fields as UNIQUE KEY in MySQL.

    My problems are:

    Even by setting both the username and email address fields as UNIQUE KEY, it is not working as I can register using the same email address or username. How to solve this?

    I have coded also to prevent this problem of duplicate so that the user will be notified to choose another email or username, but I am having a warning.

    My PHP codes:

    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset(
    $_POST['fname']) && isset($_POST['lname'])&& isset($_POST['emailr']) && isset($_POST['user']) && isset($_POST['pass'])) {
    //Assignng variables        
    $firstname mysql_real_escape_string($_POST['fname']);
    $lastname mysql_real_escape_string($_POST['lname']);    
    $email mysql_real_escape_string($_POST['emailr']);
    $uname mysql_real_escape_string($_POST['user']);
    $pwd mysql_real_escape_string($_POST['pass']);
    $pmdmd5($pwd);
    //Database
    $connect mysql_connect('localhost''root''') or die ('Connection Failed');
    mysql_select_db('registration'$connect) or die ('Connection Failed');

    //Registration codes

    if (empty($firstname) || empty($lastname) || empty($email) || empty($uname) || empty($pmd)) {
    echo 
    '<p class="error">All fields are required to fill!</p>';
    return 
    false;
    } elseif (
    strlen($firstname) && (strlen($lastname) < '2')) {
    echo 
    '<p class="error">Invalid first name or last name!</p>';
    return 
    false;
    } elseif (
    filter_var($firstnameFILTER_VALIDATE_INT) || (filter_var($lastnameFILTER_VALIDATE_INT))) {
    echo 
    '<p class="error">First name or last name cannot be integers!</p>';
    return 
    false;
    } elseif (!
    filter_var($emailFILTER_VALIDATE_EMAIL)) {
    echo 
    '<p class="error">Email address not valid!</p>';
    return 
    false;    
    } elseif (
    strlen($uname) && (strlen($pmd) < '6' )) {
    echo 
    '<p class="error">Username or password must be minimum 6 characters!</p>';
    return 
    false;
    } else {
    $query "INSERT INTO login (id, firstname, lastname, emailaddress, username, password) VALUES('', '$firstname', '$lastname', '$email', '$uname', '$pmd')";
    mysql_query($query$connect);    
    if (
    mysql_num_rows(mysql_query("SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'"))) {
    echo 
    '<p class="fail">This email or username is already taken!</p>';
    }
    }
    }
    }
    ?>
    The warning message I am getting:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\miniimagehosting\register.php on line 44

  • #2
    New Coder
    Join Date
    Feb 2012
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    you can add next condition to username and email,
    SELCT COUNT(1) FROM users WHERE username = $uname;
    if the result is equal 0, you can pass this username

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts
    you included only for username

  • #4
    New Coder
    Join Date
    Feb 2012
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    yeah, it's just example
    you can use that to username and this to mail
    SELCT COUNT(1) FROM users WHERE email = $email;
    or fallowing query to both
    SELCT COUNT(1) FROM users WHERE username = $uname OR email = $email;

  • #5
    Regular Coder Microsuck's Avatar
    Join Date
    Oct 2011
    Location
    127.0.0.1
    Posts
    123
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by angelali View Post
    I am having a big problem in MySQL and a part in my PHP coding as well.

    I created a registration form, where the user will have to fill his names, email address, choose a username and so on. I do not want duplicate email address and username in my database, thus, if a user choose either an email address or a username which has already taken, he will be notified.

    To prevent this duplication, I have set both the email address and username fields as UNIQUE KEY in MySQL.

    My problems are:

    Even by setting both the username and email address fields as UNIQUE KEY, it is not working as I can register using the same email address or username. How to solve this?

    I have coded also to prevent this problem of duplicate so that the user will be notified to choose another email or username, but I am having a warning.

    My PHP codes:

    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset(
    $_POST['fname']) && isset($_POST['lname'])&& isset($_POST['emailr']) && isset($_POST['user']) && isset($_POST['pass'])) {
    //Assignng variables        
    $firstname mysql_real_escape_string($_POST['fname']);
    $lastname mysql_real_escape_string($_POST['lname']);    
    $email mysql_real_escape_string($_POST['emailr']);
    $uname mysql_real_escape_string($_POST['user']);
    $pwd mysql_real_escape_string($_POST['pass']);
    $pmdmd5($pwd);
    //Database
    $connect mysql_connect('localhost''root''') or die ('Connection Failed');
    mysql_select_db('registration'$connect) or die ('Connection Failed');

    //Registration codes

    if (empty($firstname) || empty($lastname) || empty($email) || empty($uname) || empty($pmd)) {
    echo 
    '<p class="error">All fields are required to fill!</p>';
    return 
    false;
    } elseif (
    strlen($firstname) && (strlen($lastname) < '2')) {
    echo 
    '<p class="error">Invalid first name or last name!</p>';
    return 
    false;
    } elseif (
    filter_var($firstnameFILTER_VALIDATE_INT) || (filter_var($lastnameFILTER_VALIDATE_INT))) {
    echo 
    '<p class="error">First name or last name cannot be integers!</p>';
    return 
    false;
    } elseif (!
    filter_var($emailFILTER_VALIDATE_EMAIL)) {
    echo 
    '<p class="error">Email address not valid!</p>';
    return 
    false;    
    } elseif (
    strlen($uname) && (strlen($pmd) < '6' )) {
    echo 
    '<p class="error">Username or password must be minimum 6 characters!</p>';
    return 
    false;
    } else {
    $query "INSERT INTO login (id, firstname, lastname, emailaddress, username, password) VALUES('', '$firstname', '$lastname', '$email', '$uname', '$pmd')";
    mysql_query($query$connect);    
    if (
    mysql_num_rows(mysql_query("SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'"))) {
    echo 
    '<p class="fail">This email or username is already taken!</p>';
    }
    }
    }
    }
    ?>
    The warning message I am getting:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\miniimagehosting\register.php on line 44
    1. You need to make sure that mysql_connect() goes before mysql_real_escape_string().

    2. You gave a boolean to mysql_num_rows, you need to give it a resource.

    PHP Code:
    $result mysql_query("SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'");

    if (
    mysql_num_rows($result)) {
    echo 
    '<p class="fail">This email or username is already taken!</p>';

    Last edited by Microsuck; 02-25-2012 at 09:14 PM.
    PHP Code:
    <?php echo "Microsuck says hi!"?>

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts
    microsuck, it is not working, still got errors

    Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\miniimagehosting\register.php on line 60

  • #7
    Regular Coder Microsuck's Avatar
    Join Date
    Oct 2011
    Location
    127.0.0.1
    Posts
    123
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Try this:

    PHP Code:
    $sql "SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'";
    $result mysql_query($sql);

    if (
    mysql_num_rows($result)) {
    echo 
    '<p class="fail">This email or username is already taken!</p>';

    PHP Code:
    <?php echo "Microsuck says hi!"?>

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I have successfully solved the problem... i thank all of you who have helped me... thank guys...

  • #9
    Regular Coder Microsuck's Avatar
    Join Date
    Oct 2011
    Location
    127.0.0.1
    Posts
    123
    Thanks
    44
    Thanked 5 Times in 5 Posts
    What was your fix?
    PHP Code:
    <?php echo "Microsuck says hi!"?>

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts
    $query = "INSERT INTO login (id, firstname, lastname, emailaddress, password) VALUES('', '$firstname', '$lastname', '$email', '$pp')";
    mysql_query($query, $connect);
    if (mysql_errno($connect)> 0).................................................................................................. ................................


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •