Forum Moderators: coopster

Message Too Old, No Replies

Duplicate entry

Check to see if email is in my list

         

shaundunne

4:13 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



At the moment, if a duplicate email is put in my list, i get

Could not insert record: Duplicate entry 'email@example.com' for key 1

How can i customize this so that it can be "this email is already in use." or something along those lines.

Should i write a function to do this?

Cheers

[edited by: eelixduppy at 4:23 pm (utc) on Dec. 27, 2007]

PHP_Chimp

4:23 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could write a separate function to deal with this, but it may well just be a single line of added code to whatever you already have.

// connect to database
// do anything else you want
$insert = *sql_query('INSERT INTO information(email) VALUES 'me@example.com');
if (!$insert) {
echo 'Didnt work...Have a nice life ;)';
}

Iv used *sql_query as you could be on a lot of different databases, but I guess that you want mysql_query.
Hopefully this will give you an idea where to place the if (!$working) statement in your code. If you cant get it to work then post the part of your code where you are doing the inserts.

henry0

4:23 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could first query the DB
if it exists: echo a message with a link back to the form
and exit()

For further help we need looking at the form and script excerpts pertaining to your quest

shaundunne

4:33 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



My script is as follows

<?php
//functions to use
//email validator
function check_email_address($email) {
// check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{¦}~-][A-Za-z0-9!#$%&'*+/=?^_`{¦}~\.-]{0,63})¦(\"[^(\\¦\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])¦([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
//postcode validator
function IsPostcode($postcode) {
$postcode = strtoupper(str_replace(chr(32),'',$postcode));
if(ereg("^(GIR0AA)¦(TDCU1ZZ)¦((([A-PR-UWYZ][0-9][0-9]?)¦"
."(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)¦"
."(([A-PR-UWYZ][0-9][A-HJKSTUW])¦"
."([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))"
."[0-9][ABD-HJLNP-UW-Z]{2})$", $postcode))
return $postcode;
else
return FALSE;
}
//connect to server and select database
$mysql = mysql_connect("localhost","user","pass");
mysql_select_db("db");
//if connection connection fails
if (!$mysql) {
echo "connection failure";
exit();
//insert into table
} else{

if(check_email_address($email) === TRUE){

if(IsPostcode($postcode)!= FALSE){

$fname = $_POST['firstname'];
$sname = $_POST['surname'];
$addr1 = $_POST['addr1'];
$addr2 = $_POST['addr2'];
$addr3 = $_POST['addr3'];
$town = $_POST['town'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$sql = "INSERT INTO collection (firstname,surname,addr1,addr2,addr3,town,county,postcode,email) VALUES('$fname','$sname','$addr1','$addr2','$addr3','$town','$county','$postcode','$email')";
$res = mysql_query($sql,$mysql);
}
}

//success or failed messages
if(check_email_address($email) === FALSE)
{
echo "Email is invalid";
}
if(IsPostcode($postcode) === FALSE)
{
echo "Postcode is invalid";
}

if ($res === TRUE) {
echo "A record has been inserted";
} else {
echo "Could not insert record:\n".mysql_error();
mysql_close($mysql);
}
}
?>

I want to have a message saying that the email is already in use/signed up.

As i have two functions in use , should i just write another or is there a simpler way of doing it?

Cheers

henry0

4:47 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



as per PHP_Chimp

$sql = "INSERT INTO collection (firstname,surname,addr1,addr2,addr3,town,county,postcode,email) VALUES('$fname','$sname','$addr1','$addr2','$addr3','$town','$county','$postcode','$email')";
$res = mysql_query($sql,$mysql);
if(!$res)
{
echo"email exists <a href='asasa.php'>enter a unique email";
exit(); // stop script further exec
}

}
}

but "!res" could be for another reason
so I will rather first check if that email exists
then send that message.
start writing it
if you need a hand let us know

shaundunne

4:49 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Cheers for your help

ill get onto this first thing as i have to go home now.

Thanks again.

PHP_Chimp

4:50 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have almost got what I was trying to show in there already :)

It would be worth checking the return values for the mysql_query [uk.php.net] in the manual as you are checking for TRUE...and a query like you are using doesnt return true :(
From the manual -

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE

if ($res === TRUE) {
echo "A record has been inserted";
} else {
echo "Could not insert record:\n".mysql_error();
mysql_close($mysql);
}

So should be -

if ($res === false) {
echo "This didnt work.\nA team of highly trained monkeys has been dispatched to deal with the situation.";
}
else {
echo 'It worked!';
}

<edit>
Beaten by 3 mins :(

[edited by: PHP_Chimp at 4:53 pm (utc) on Dec. 27, 2007]

henry0

5:44 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP_Chimp
You are always very thorough
I use <div class="telegram_style">my stuff</div>" :)

shaundunne

1:51 pm on Dec 28, 2007 (gmt 0)

10+ Year Member



thanks guys, got this working using --

if ($res === TRUE) {
echo "A record has been inserted";
}
if ($res == 0) {
echo "you have already subscribed you monkey";

thank you,