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 15 of 15
  1. #1
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function no longer working / wont check if email already exists

    Hey guys,

    i have been working on a log in / register system for a couple days now, and i am in the process of finalizing my end users activation of a newly registered account.

    I was having no real issues until now, to where i am utterly confused as to why i am having such a core problem.

    So for the first part i had a user register, and wanted to make sure the user is not already entering an email that already exists in my Database.


    PHP Code:
    if (email_exists($_POST['register_email']) === true) {
    $errors[] = 'The email you provided is already in use. If you are having trouble remembering your user info click <a href="#">here</a>';


    the error goes to an errors array i have setup with all the errors that would display to the end user if any occur.

    the email_exists() is the following :

    PHP Code:
    function email_exists($email) {


    $username sanitize($email);
    $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' ");
    $results mysql_query($query);


    if(
    mysql_error()) {
    echo 
    'MySQL Error: 'mysql_error() .'<br>';
    echo 
    'Query: '.$query;
    }  else if(!
    mysql_num_rows($results)) {
    //###  row has not been returned 
    }  else {
    //### row has been returned
    }


    return (
    mysql_result($results0) == 1) ? true false;


    Now everything in the function calls correctly, and as you see in the error check, row will be returned.

    Yet, as you see in my IF statement in the error check above, nothing is being returned with the variable i am inputting ( yet the other IF statement all seem to work with the same variable $_POST)

    now this i figured was the problem to my MAIN and number one issue. Getting my user activation working.

    PHP Code:
    if(isset($_GET['success']) === true && empty($_GET['success']) === true){ 
    ?>
    <h2> Thank you, your account has been activated</h2>
    <h3> You can now log in and access the site and join the disscussions in out forum !</h3>
    <?
    } else if (isset($_GET['email'], $_GET['email_code']) === true){ 
    $email  =  trim($_GET['email']); 
    $email_code trim($_GET['email_code']);


    if (
    email_exists($email) === false ){
    $errors[] = ' We have a problem, we couldn\'t find that email address !'// THIS IS THE ERROR GIVEN TO USER
    } else if (activate($email$email_code) === false) {
    $errors[] = ' We had a problem activating your account ';
    }
    if(empty(
    $errors) === false){
    ?>
    <h2>Oops...</h2>
    <?
    echo output_errors($errors);
    } else {
    header('Location: activate.php?success');
    exit();
    }


    } else {
    header('Location:  index.php');
    exit();
     }

    Now above i have highlighted what conditional is passed, when a end user click on the return link to activate their account.

    as i mentioned above , i know its the email_exists function, since not only does that conditional fail . in database i can create multiple users with the same email.

    Please guys any help would be greatly appreciated.

    I am a PHP / Programming noob , but i am trying my best to debug and catch my issues. i just cant figure this one out on my own .

  • #2
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Your email checking code seems too cumbersome to me and you don't seem to be using your variable $username anywhere.

    This, untested, code might be easier.

    PHP Code:
    <?php

    function DoesEmailExist($email) {
        
    $query 'select email from users where email = ' "'" mysql_real_escape_string($email) . '"';
        if (!
    $rs mysql_query($query)) {
            return -
    1;  //cannot run query to check email
        
    } else {
            if (!
    $numRows mysql_num_rows($rs)) {
                return -
    2;  //cannot check the number rows in result set
            
    } else {
                return (
    $numRows == 0) ? false true;
            }
        }
    }
    ?>

  • #3
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Knight,

    Thanks for the reply, i really do appreciate the feedback.

    i did notice i had a $username variable undefined in that code, that was something i caught myself and changed to $email.. the reason that was there was due to me copying an identical function that was user_exist function

    anyway , i tried doing what you mentioned with no success.

    i still am able to repeat users with the same emails able to registers. the query to the DB and the fields are correct. the variables are able to be read and are defined properly(to my knowledge), and the function works exactly as i have it for the username .. which just baffles me more.

    any more suggestions ?

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by justin7410 View Post
    i still am able to repeat users with the same emails able to registers.
    the easy solution: (in the DB), make the email field UNIQUE, then it’s impossible to register with the same email another time.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by justin7410 View Post
    anyway , i tried doing what you mentioned with no success.
    But you haven't showed how you tried what I posted.

    In any case, the logic in your registration script needs to be something only like this:

    PHP Code:
    $emailExists DoesEmailExist($email);

    if(
    $emailExists  === false || $emailExists == -|| $emailExists == -2){
         
    //error handling code goes here
    } else {
        
    // code to register user goes here

    Obviously you'll need to add data validation and db connection code at the top of the script.

  • #6
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Edit:

    PHP Code:
    if($emailExists  === false 
    should be

    PHP Code:
    if($emailExists  === true 

  • #7
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by knightCoder View Post
    But you haven't showed how you tried what I posted.

    In any case, the logic in your registration script needs to be something only like this:

    PHP Code:
    $emailExists DoesEmailExist($email);

    if(
    $emailExists  === false || $emailExists == -|| $emailExists == -2){
         
    //error handling code goes here
    } else {
        
    // code to register user goes here

    Obviously you'll need to add data validation and db connection code at the top of the script.

    Hey Knight,

    man i really appreciate the help, but your code now instead of allowing all emails to be registering now does the opposite and says every email entered already is in the database.

    Let me backtrack for you a bit:

    First, inserted your code:

    PHP Code:
    function DoesEmailExist($email) {
        
    $query 'select email from users where email = ' "'" mysql_real_escape_string($email) . '"';
        if (!
    $rs mysql_query($query)) {
            return -
    1;  //cannot run query to check email
        
    } else {
            if (!
    $numRows mysql_num_rows($rs)) {
                return -
    2;  //cannot check the number rows in result set
            
    } else {
                return (
    $numRows == 0) ? false true;
            }
        }

    followed by this code in the register.php file

    PHP Code:
    $emailExists DoesEmailExist($email);

    if(
    $emailExists  === false || $emailExists == -|| $emailExists == -2){
    $errors[] = 'Sorry this email already exists in our Database';
            }else{ die(
    'Your form is done'); }

    i then replaced ($email) with $emailExists =
    DoesEmailExist($_POST['register_email']);
    This got me to the point where i was not allowing anyone to register.

    I then replaced your query of :
    $query = 'select email from users where email = ' . "'" . mysql_real_escape_string($email) . '"';
    to

    $query = 'SELECT COUNT(`user_id`) FROM `users` WHERE `email` = ' . "'" . mysql_real_escape_string($email) . '"';

    still fail

    i then switched to my original query of:

    $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' ");

    which passed.

    since i replaced -1 and -2 with die() to find the exact position. the query was not matching. I am new to php so i was not sure if you purposely did that for me to add my own query or not. Anyway, after i added my own query to then pass the -1, -2 returns , i still get the final message of "this email is already in use".

  • #8
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' "); 
    SHOULD BE

    $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' ");

  • #9
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by justin7410 View Post
    but your code now instead of allowing all emails to be registering now does the opposite and says every email entered already is in the database.
    Not sure what you mean by MY code. It's actually YOUR code because the code I posted doesn't allow or disallow anything to be registered. All the function I posted does is check if an email exists in the db or not.

    A couple of points -

    1) you seem to have ignored the edit I made in post 6

    2) have you checked that the returned value from DoesEmailExist() is actually correct for the input email address. To test it, input a new email address and an address that already exists in the db and make sure the correct true or false values are being returned. I did say in my original post that the code is untested. I don't have time to set up a database table and code to test the function.

  • #10
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Quote Originally Posted by knightCoder View Post
    Not sure what you mean by MY code. It's actually YOUR code because the code I posted doesn't allow or disallow anything to be registered. All the function I posted does is check if an email exists in the db or not.

    A couple of points -

    1) you seem to have ignored the edit I made in post 6

    2) have you checked that the returned value from DoesEmailExist() is actually correct for the input email address. To test it, input a new email address and an address that already exists in the db and make sure the correct true or false values are being returned. I did say in my original post that the code is untested. I don't have time to set up a database table and code to test the function.
    LOL yes this is my code, when i was referencing your code, i simply meant the code you supplied.

    regarding

    PHP Code:
    if($emailExists  === false 
     
    should be

    PHP Code
    :
    if(
    $emailExists  === true 
    i did set the code to true, although that did not make much of a difference.

    and to your second point/question . i do have the database setup and i do have the actual register function working. the conditional i had originally was allowing a user to be created regardless if the user is already inputted into the database. after altering it and adding your snippet and adding the same functions and conditional logic , it wont let me register either a user not in the Database or a user that is in the database(which is what i wanted in the first place).

    i really do appreciate all your help man , i have been following a tutorial and my original logic is working for other people. i just cant figure this out

  • #11
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    To debug your code properly you need to di it step by step until you find the source of a problem.

    It appears you haven't checked yet if the output of the email address cheking function is correct or not for the input. So that is the 1st thing you should do, especially when I said the code I posted is not tested.

    Add this debugging code
    PHP Code:
    $emailExists DoesEmailExist($email); 

    echo 
    'emailExists = '.$emailExists//debugging code 
    This will output the actual returned value from the function. Input a new $email value and an existing $email value and make sure the returned value from the function is correct. If it is then the problem is downstream in your code. If it isn't, then there is an error in function's code.


  • #12
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Getting my user activation working.

  • #13
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey guys,

    i still have no luck with this issue. as simple as it should be to fix

    im guessing my lack of php and programming knowledge is handicapping me from being able to get the problem solved.

    i appreciate all the feedback, if could get back to this problem and to find a solution.

    so i have a very simple function called email_exists();
    PHP Code:
    function email_exists($email) {
    $email sanitize($email);
    return (
    mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'"), ) == 1) ? true false
    now the query has been checked and is returned ok.

    i then get to the register page:


    PHP Code:
    if (empty($errors) === true) {
    if (
    user_exists($_POST['register_username']) === true) {
    $errors[] = 'Sorry, the username \'' $_POST['register_username'] . '\' has already been taken. Please choose another username.';
    }
    if (
    preg_match("/\\s/"$_POST['register_username']) == true) {
    $errors[] = 'Sorry your username must not contain any spaces!';
    }
    if (
    strlen($_POST['password']) < ){ 
    $errors[] = 'Your Password must be a minimal 6 characters long';
    }
    if (
    $_POST['password'] !== $_POST['confirm_pass']) {
    $errors[] = 'Your Passwords do not match, please make sure both passwords are the same!';
    }
    if (
    filter_var($_POST['register_email'], FILTER_VALIDATE_EMAIL) === false) {
    $errors[] = 'A valid email adress is required';
    }
    if(
    email_exists($_POST['register_email']) === true){  // THIS CONDITIONAL DOES NOT PASS
    $errors[] = 'The email you provided is already in use. If you are having trouble remembering your user info click <a href="#">here</a>';
            
             }
      }
    }
    print_r($errors);
    print_r($_POST['reigster_email']); 
    Now when i print both these things out, no error is showing , when i use the same email that already exists in the database. Yet, the variable of the $_POST shows the email being passed correctly.

    The reason as to why i am so utterly confused is that , i have an exact duplicate function that checks if an username is already taken.


    PHP Code:
    function user_exists($username) {
     
    $username sanitize($username);
    $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' ");
    $results mysql_query($query);


    if(
    mysql_error()) {
    echo 
    'MySQL Error: 'mysql_error() .'<br>';
    echo 
    'Query: '.$query;
    }  else if(!
    mysql_num_rows($results)) {
    //###  row has not been returned 
    }  else {
    //### row has been returned
    }


    return (
    mysql_result($results0) == 1) ? true false;

    same logic different variables. this passes with no problems and everything works fine.

    i am beyond frustrated at this point. and my lack of programming knowledge and php knowledge is somewhat handicapping me with a solution from the forums.

    any help or suggestions... better yet a solution would be GREATLY appreciated.

    thanks guys

  • #14
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey guys,

    i still have no luck with this issue. as simple as it should be to fix

    im guessing my lack of php and programming knowledge is handicapping me from being able to get the problem solved.

    i appreciate all the feedback, if could get back to this problem and to find a solution.

    so i have a very simple function called email_exists();
    PHP Code:
    function email_exists($email) {
    $email sanitize($email);
    return (
    mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'"), ) == 1) ? true false
    now the query has been checked and is returned ok.

    i then get to the register page:


    PHP Code:
    if (empty($errors) === true) {
    if (
    user_exists($_POST['register_username']) === true) {
    $errors[] = 'Sorry, the username \'' $_POST['register_username'] . '\' has already been taken. Please choose another username.';
    }
    if (
    preg_match("/\\s/"$_POST['register_username']) == true) {
    $errors[] = 'Sorry your username must not contain any spaces!';
    }
    if (
    strlen($_POST['password']) < ){ 
    $errors[] = 'Your Password must be a minimal 6 characters long';
    }
    if (
    $_POST['password'] !== $_POST['confirm_pass']) {
    $errors[] = 'Your Passwords do not match, please make sure both passwords are the same!';
    }
    if (
    filter_var($_POST['register_email'], FILTER_VALIDATE_EMAIL) === false) {
    $errors[] = 'A valid email adress is required';
    }
    if(
    email_exists($_POST['register_email']) === true){  // THIS CONDITIONAL DOES NOT PASS
    $errors[] = 'The email you provided is already in use. If you are having trouble remembering your user info click <a href="#">here</a>';
            
             }
      }
    }
    print_r($errors);
    print_r($_POST['reigster_email']); 
    Now when i print both these things out, no error is showing , when i use the same email that already exists in the database. Yet, the variable of the $_POST shows the email being passed correctly.

    The reason as to why i am so utterly confused is that , i have an exact duplicate function that checks if an username is already taken.


    PHP Code:
    function user_exists($username) {
     
    $username sanitize($username);
    $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' ");
    $results mysql_query($query);


    if(
    mysql_error()) {
    echo 
    'MySQL Error: 'mysql_error() .'<br>';
    echo 
    'Query: '.$query;
    }  else if(!
    mysql_num_rows($results)) {
    //###  row has not been returned 
    }  else {
    //### row has been returned
    }


    return (
    mysql_result($results0) == 1) ? true false;

    same logic different variables. this passes with no problems and everything works fine.

    i am beyond frustrated at this point. and my lack of programming knowledge and php knowledge is somewhat handicapping me with a solution from the forums.

    any help or suggestions... better yet a solution would be GREATLY appreciated.

    thanks guys

  • #15
    New Coder
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so i found out the solution to this issue.

    its quite simple. i have an extra space in the field with the email being submitted.

    i know i need to use trim(), to do this.

    just wanted to solve the issue


  •  

    Tags for this Thread

    Posting Permissions

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