Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2012, 12:13 PM   PM User | #1
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Unhappy Duplicate content problem!

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:

HTML codes:

<form action="index.php" method="post">
<input type="text" size="25" placeholder="Your email address..." name="enter"/>
<input class="submit" type="submit" value="Subscribe" name="subscribe"/>
<br/>

PHP with Query codes:

<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$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...Try again!</p>';
} else {
@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!');
$duplicate = "SELECT * FROM `email` WHERE `emailaadress` = '{$ee}'";
$query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
$result = mysql_query($duplicate);
if ( mysql_num_rows ( $result ) > 1) {
/* Username already exists */
echo 'Username already exists';
} else {
mysql_query($query);
echo '<p class="success">Successfully subscribed!</p>';
}
}
}
?>
</form>

Error I am having:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\softwareportfolio\index.php on line 68

Can someone help me? Thank
angelali is offline   Reply With Quote
Old 02-20-2012, 12:39 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Change
PHP Code:
$result mysql_query($duplicate); 
to
PHP Code:
$result mysql_query($duplicate) or die(mysql_error()); 
to check for any errors int he query.

btw, you could set a primary/unique key to the email field, to keep them unique.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 02-20-2012, 02:55 PM   PM User | #3
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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?
angelali is offline   Reply With Quote
Old 02-20-2012, 03:00 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Well I ma getting another errors now...
Tyy to fix it first.

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)
abduraooft is offline   Reply With Quote
Old 02-20-2012, 03:00 PM   PM User | #5
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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?
angelali is offline   Reply With Quote
Old 02-21-2012, 04:42 AM   PM User | #6
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Have you read my above post?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 02-21-2012, 06:39 AM   PM User | #7
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Yes, I hav tried to search on it but in vain. I am new in PHP that's why I asked how to do it
angelali is offline   Reply With Quote
Old 02-21-2012, 07:12 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-21-2012, 07:32 AM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 02-21-2012, 07:51 AM   PM User | #10
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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'
angelali is offline   Reply With Quote
Old 02-21-2012, 08:10 AM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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($eeFILTER_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' );
}
}
}
?>
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 02-21-2012, 08:14 AM   PM User | #12
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Code:
$duplicate = "SELECT * email WHERE emailaddress";
if ($duplicate === $ee) {
echo "Your email is already registered!";
Don't you need to _query the DB using that sql and fetch the data from the result set?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 02-21-2012, 08:18 AM   PM User | #13
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
@FELGALL, I am getting this error:

Fatal error: Call to undefined function mysql_errorno() in C:\xampp\htdocs\softwareportfolio\index.php on line 58
angelali is offline   Reply With Quote
Old 02-21-2012, 08:22 AM   PM User | #14
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
I have changed it to: (mysql_errno() == 1022), still not work... i am searching on this functionality since morning!
angelali is offline   Reply With Quote
Old 02-21-2012, 09:04 AM   PM User | #15
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
I have tried to make some more corrections, still not work, here are 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 mysql_real_escape_string($_POST['enter']);
$emailCheck mysql_query("SELECT emailaddress FROM email WHERE emailaddress = '".$ee."'");
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;

elseif (
mysql_num_rows($emailCheck) > 0) {
echo(
"EMAIL Already exists");
}
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' );
}
}
?>
angelali is offline   Reply With Quote
Reply

Bookmarks

Tags
duplicate, email address, mysql, store

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:13 AM.


Advertisement
Log in to turn off these ads.