Jump to content

Duplicate content in database!


angelali

Recommended Posts

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

Link to comment
Share on other sites

In your code here, $result is the result of mysql_query.  $result is either:

A)  A mysql_resultset resource, on which you can call mysql_fetch_array, mysql_num_rows, etc.

B)  false, which indicates a query error.

 

So:

 

if ( $result == false ) {
  echo "THERE WAS AN ERROR: " . mysql_error();
  die();
}

Of course, there are far better ways to handle errors, but this will actually show you on the screen what the error is.  You should also echo the query itself, so it shows up alongside the error and you can see if something is misspelled, like it is in this case.

 

-Dan

Link to comment
Share on other sites

Yes, I corrected the error of $duplicate. Again the problem still here.. :( Here the codes you stated to me to correct:

 

<?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 `emailaddress` = '.$ee.'";
$query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
$result = mysql_query($duplicate);
if ( $result == false ) {
echo "THERE WAS AN ERROR: " . mysql_error();
die();
} else {
mysql_query($query);
echo '<p class="success">Successfully subscribed!</p>';
}
}
}
?>

Link to comment
Share on other sites

Perhaps...

<?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>';
		die();
	/* send them back to form */
}
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 `emailaddress` = '$ee'";
$query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '$ee')";
$result = mysql_query($duplicate);
if(mysql_num_rows>0) {
	echo "That address already exists";
	die();
	/* send them back to form */
}
$result2 = mysql_query($query);
if ( $result2 == false ) {
	echo "THERE WAS AN ERROR: " . mysql_error();
	die();
	/* send them back to form */
}
echo '<p class="success">Successfully subscribed!</p>';
}
?>

Link to comment
Share on other sites

I got this Litebearer:

 

Notice: Use of undefined constant mysql_num_rows - assumed 'mysql_num_rows' in C:\xampp\htdocs\softwareportfolio\index.php on line 59

THERE WAS AN ERROR: Duplicate entry 'alithebest@gmail.com' for key 'emailaddress'

 

Still trying to figure out...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.