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>';
}
}
}
?>
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