Jump to content

[SOLVED] Help with checking data


felipeebs

Recommended Posts

Hi, I'm making a php/MySQL newsletter system and I want to know how to, before inserting the data in the database, check if the e-mail is already stored... something like this:

 

if email is already in the database returns "E-mail already in database!"

else INSERT INTO `newsletter` VALUES ('[email protected]', 'name'); than returns "E-mail stored sucessfully, thanks!"

 

The e-mail validation is already done, but the system is accepting same e-mail multiple times

 

If someone can help... Thanks ;)

 

PS: I don't know if it's the right place to post and my english os horrendous  :(

Link to comment
https://forums.phpfreaks.com/topic/44442-solved-help-with-checking-data/
Share on other sites

Sorry... delayed... now I'm posting the code

 

First the form I called "newsletter.html":

 

<form id="newsletter" method="GET" name="newsletter" action="insert.php">
   <fieldset>
   <legend>Sign in our  Newsletter</legend>
   <label>Name 
   <input id="name" name="name" /> 
   </label><br />
   <label>E-mail 
   <input id="email" name="email" /><br />
   <input id="sign" type="submit" value="Sign" /> 
   </label></fieldset>
</form>

 

And this is the code for "insert.php":

<?
# Declarating variables
$name = $_GET['name'];
$email = $_GET['email'];

$normal = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; // E-mail validation (looks ugly huh?)

if ($name == "") { // Name is null
echo "What is your name?";
}
elseif ($email == "") { // Email is null
echo "What is your e-mail?";
}
elseif (eregi($normal, $email) && $name !="") { // E-mail validation
$conexao = mysql_connect("localhost","root","schmitz");
mysql_select_db("felipeebs");

# Inserting values to the database
$sql = "INSERT INTO newsleter (email, name) VALUES ('$email','$name')";

$query = mysql_query($sql);

# Após inserir os dados
echo "E-mail added sucessfull!";
} else {
echo "Check your e-mail adress..."; // Oops... the e-mail is not valid
}

?>

 

EDIT:

Almost forgot the SQL table structure...

 

CREATE TABLE `newsleter` (
  `email` varchar(250) NOT NULL default '',
  `name` varchar(250) NOT NULL default '',
  PRIMARY KEY  (`email`)
) TYPE=MyISAM;

<?
# Declarating variables
$name = $_GET['name'];
$email = $_GET['email'];

$normal = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; // E-mail validation (looks ugly huh?)

if ($name == "") { // Name is null



echo "What is your name?";
}
elseif ($email == "") { // Email is null



echo "What is your e-mail?";
}
elseif (eregi($normal, $email) && $name !="") { // E-mail validation



$conexao = mysql_connect("localhost","root","schmitz");



mysql_select_db("felipeebs");


$emailCheck = mysql_query("SELECT email FROM newsleter WHERE email = '".$email."'");
if (mysql_num_rows($emailCheck) > 0) {
     die("EMAIL Already exists");
}

# Inserting values to the database
$sql = "INSERT INTO newsleter (email, name) VALUES ('$email','$name')";



$query = mysql_query($sql);



# Após inserir os dados



echo "E-mail added sucessfull!";
} else {



echo "Check your e-mail adress..."; // Oops... the e-mail is not valid
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.