PDA

View Full Version : How to check if Database already contains email address


tux
10-01-2008, 07:57 AM
Hi all,

I need to check if an email address is already contained in my databse.

What is the best way to do this in PHP.

The user will be completing a form and I want to stop them if their email address has already been used.

Thanks all.

Regards, Paul

lux
10-01-2008, 10:31 AM
1. capture the form data inputted by the user

2. query the database for all the existing emails of existing accounts (db rows)

3. loop through the results of the query and compare the stored email address to the email address supplied in the form
- convert the emails to lowercase and then compare

4. if a match is found highlight the fact by changing the value of a variable

5. in your form validation test the value of the above variable if corresponds to a found email inform the user and ask them to enter a different email address

gavimobile
10-01-2008, 01:33 PM
Hi all,

I need to check if an email address is already contained in my databse.

What is the best way to do this in PHP.

The user will be completing a form and I want to stop them if their email address has already been used.

Thanks all.

Regards, Paul

tux, i just did something like this. I hope i explained it good enough, let me know if something is unclear!



<?php
//if form has been submitted
if (isset($_POST['Submit'])){

$email = mysql_real_escape_string($_POST['email']);
$results = mysql_query("select id from users where user_email='$email' ");
$row = mysql_num_rows($results);
if ($row > 0 ) {
//if $row is greater than 0, (means the email exists)
echo "Error: email already exists";
} else {

if ($row == 0 ) {
// $row is equal to 0, (==), this means it didnt find results (email)
//echo "Email does not exists, so lets add the email to the database";
mysql_query("INSERT INTO users (user_email) VALUES ('$email')");
echo "email has been added to the database";
}

}
?>

lux
10-01-2008, 01:53 PM
Good effort, you need to check your if statements.

Here's an example to work from:


if (isset($_POST['Submit'])){

//remove any whitespace and convert characters to lowercase
$email = strtolower(trim($_POST['email']));

//prepare $email to be used in a db query
//convert user_email to lowercase and compare against $email
$query = sprintf("select id from users where lower(user_email)= '%s' ", mysql_real_escape_string($email));

$result = mysql_query($query);

$num_rows = mysql_num_rows($result);


if($num_rows > 0)
{
$message = "Error: email already exists.";
}
elseif($num_rows == 0)
{
$query = sprintf("INSERT INTO users (user_email) VALUES ('%s')", mysql_real_escape_string($email));

$execute = mysql_query($query);

$message = "Success: email has been recorded";
}

echo $message;
}

gavimobile
10-01-2008, 02:29 PM
Good effort, you need to check your if statements.

Here's an example to work from:


if (isset($_POST['Submit'])){

//remove any whitespace and convert characters to lowercase
$email = strtolower(trim($_POST['email']));

//prepare $email to be used in a db query
//convert user_email to lowercase and compare against $email
$query = sprintf("select id from users where lower(user_email)= '%s' ", mysql_real_escape_string($email));

$result = mysql_query($query);

$num_rows = mysql_num_rows($result);


if($num_rows > 0)
{
$message = "Error: email already exists.";
}
elseif($num_rows == 0)
{
$query = sprintf("INSERT INTO users (user_email) VALUES ('%s')", mysql_real_escape_string($email));

$execute = mysql_query($query);

$message = "Success: email has been recorded";
}

echo $message;
}

lux, nice one.... ill be in your shoes soon enough;-)!!!

lux
10-01-2008, 03:12 PM
at size 12 (uk) thats a big pair of shoes! hehe

my best advice is use php.net as must as possible!

gavimobile
10-01-2008, 03:23 PM
at size 12 (uk) thats a big pair of shoes! hehe

my best advice is use php.net as must as possible!

thats exactly what ive been doing!
im gona make a new post because there is something i am not understanding in the manual. if u get a chance take a look,

thanks lux,
gavimobile

davidj
10-01-2008, 05:19 PM
where you have this



if($num_rows > 0){

$message = "Error: email already exists.";

}elseif($num_rows == 0){

$query = sprintf("INSERT INTO users (user_email) VALUES ('%s')", mysql_real_escape_string($email));

$execute = mysql_query($query);

$message = "Success: email has been recorded";

}



is the elseif really needed


elseif($num_rows == 0){

lux
10-01-2008, 06:39 PM
no not really, sorry boss! (hehe)

gavimobile
10-01-2008, 06:47 PM
no not really, sorry boss! (hehe)
so that makes my post fine then, no?