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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PhP/MySQL - Checking if an email exists in a database

    Hi,

    I am trying to get some code straight. I know this is a complete and utter mess, my apologies (newbie coder). The aim of the script is that if the email matches a regex expression (previously defined), then it checks the database to see if the email the user has entered is already there. If it is, it prints back a message telling the user to enter a new one.

    The database field itself is unique, so technically there will be no other email that is the same, however for the sake of having to redirect the user back I'd like to just keep all the validation on one page.

    However, when I enter an email that already exists in the database, it still lets me continue to the database page. Not sure why this is, can anyone help me out?

    Thanks

    -------------------------------
    Code:
    <?php
    //Email Validation
    $email = clean($_POST['Email1']);
    $pattern = '/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-…
    if ($emailpattern=(preg_match($pattern, $_POST['Email1'])) > 0){
    $emailclass = "basictext";
    $flag1="OK";
    }
    else{
    $emailclass = "errortext";
    $flag1="NOTOK";
    $emailvalidationcheck ="NOTOK";
    }
    ?>
    
    
    <tr>
    <td><div class="<?php print $emailclass; ?>">Email Address:*</div></td>
    <td><input type="text" name="Email1" value="<?php print $email; ?>" size="30"/></td>
    </tr>
    
    <?php
    
    if ($emailvalidationcheck == "NOTOK"){
    	$flag = "NOTOK";
        print '<tr><td align="center" colspan="2" class="errorsubtext"> Please enter a valid Email Address. </td></tr>'; 
        }
    	else if ($emailvalidationcheck == "OK"){
    	
    	$host="***********"; // Host name
        $username="*********"; // Mysql username
        $password= (DATABASE_PASSWORD); // Mysql password
        $db_name="********"; // Database name
        $tbl_name="customers"; // Table name 
        
    	// Connect to server and select databse.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");
    	$sql = mysql_query( "SELECT `customers_email_address` FROM `".$tbl_name."` WHERE `customers_email_address` = \'" . mysql_real_escape_string( $email ) . "\' LIMIT 1" );
        $result = mysql_fetch_row($sql);
    	mysql_free_result($result);
    	if(isset($result[customers_email_address])){	
    	$emailclass = "errortext";
    	$flag="NOTOK";
    	print '<tr><td align="center" colspan="2" class="errorsubtext"> This email address is already in use. </td></tr>';
        }
    	else if(!isset($result[customers_email_address])){
    	$emailclass = "basictext";
    	$flag="OK";
        }
    	}
    
    ?>
    Last edited by jj24jj; 03-11-2010 at 01:12 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Don't escape the single quotes in your query. Why did cut out parts of your code? The syntax highlighting is a little off.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry, that was just me being a nab, trying to figure out the forum layout :P should all just be in one place now.
    Thanks for the suggestion, though unfortunately it is still not working. Not sure whats going on.
    Last edited by jj24jj; 03-10-2010 at 04:36 AM.


  •  

    Posting Permissions

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