I have created an input field on a website for people to subscribe by their email address. The email address is stored in a database. I am using PHPMyAdmin.
The email address is successfully working, but I want to prevent duplicate email address to be stored, however, I am having an error. Here are my codes:
Well I ma getting another errors now... But I will apply what you said, can you tell me what you mean by applying a primary/unique key for the field in my table? You mean, I put a primary of it just like I did for ID?
If you already have a primary key, then set a unique key to the email field. After that you may simply run the insert query and analyse the output of mysql_errno() to inform the user.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
Ok, I made it unique the field for email address in my table, and it is working I see. Can I know how to tell the user that his email address is already registered if he tries to subscribe again? I mean how to do that to make him aware?
The easiest way is to first do a SELECT query to see if the email address is already in the DB table.
Yes, you can and should make the email address a unique key, but then you will have to parse the error message you would get from your INSERT query to make sure that the duplicate key is the reason for the error.
Much simpler to just make the SELECT test, first.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
If you have it set up as a unique key then the easiest way is to simply try to insert whatever email address they enter. If the insert works then it isn't a duplicate - if the insert fails then you can produce appropriate error handling to advise them of the duplication in the die clause.
Doing it that way halves the number of database calls to perform the test and is therefore much faster.
It is just the same as where you want to update a record if it already exists or insert it if it doesn't where the most efficient way is to pick whichever of insert or update you expect to work more often than not and call the other if that one fails (rather than the far less efficient way of doing a select first to see whether the record already exists).
For the greatest efficiency of your database accesses set up the appropriate keys and make use of error handling when database queries fail rather than making extra unnecessary calls that slow things down.
I have tried to do it, but I think I did it bad, here the codes:
PHP Code:
<?php if ( $_SERVER['REQUEST_METHOD'] == "POST" ) { @mysql_connect ('localhost', 'root', '') or die ('A problem has occurred, refresh the page and try again!'); @mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!'); $ee = htmlentities($_POST['enter']); if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){ echo '<p class="fail">Failed! You will be redirected in 3 seconds!</p>'; header( 'refresh:3; url= index.php' ); return false; } else { $duplicate = "SELECT * email WHERE emailaddress"; if ($duplicate === $ee) { echo "Your email is already registered!"; } else { $query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')"; mysql_query($query); echo '<p class="success">Successfully subscribed!</p>'; header( 'refresh:2; url= index.php' ); } } } ?>
*Note, my database name is 'links', the table is 'email' and the email field is 'emailaddress'
Assuming that you have the unique key set up on the field the following should do what you want
PHP Code:
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
@mysql_connect ('localhost', 'root', '') or die ('A problem has occurred, refresh the page and try again!');
@mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!');
$ee = $_POST['enter'];
if(!filter_var($ee, FILTER_VALIDATE_EMAIL)){
echo '<p class="fail">Failed! You will be redirected in 3 seconds!</p>';
header( 'refresh:3; url= index.php' );
return false;
} else {
$query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
mysql_query($query);
if (mysql_errorno() == 1022) {
echo "Your email is already registered!";
} else {
echo '<p class="success">Successfully subscribed!</p>';
header( 'refresh:2; url= index.php' );
}
}
}
?>