felipeebs Posted March 27, 2007 Share Posted March 27, 2007 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 More sharing options...
mmarif4u Posted March 27, 2007 Share Posted March 27, 2007 Post ur code of validation... Link to comment https://forums.phpfreaks.com/topic/44442-solved-help-with-checking-data/#findComment-215849 Share on other sites More sharing options...
jitesh Posted March 27, 2007 Share Posted March 27, 2007 Placed your code here. Link to comment https://forums.phpfreaks.com/topic/44442-solved-help-with-checking-data/#findComment-215852 Share on other sites More sharing options...
felipeebs Posted March 27, 2007 Author Share Posted March 27, 2007 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; Link to comment https://forums.phpfreaks.com/topic/44442-solved-help-with-checking-data/#findComment-216151 Share on other sites More sharing options...
per1os Posted March 27, 2007 Share Posted March 27, 2007 <? # 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 } ?> Link to comment https://forums.phpfreaks.com/topic/44442-solved-help-with-checking-data/#findComment-216168 Share on other sites More sharing options...
felipeebs Posted March 27, 2007 Author Share Posted March 27, 2007 Thanks a lot Now I have to start the integration with AJAX... but it is a post to an other forum Link to comment https://forums.phpfreaks.com/topic/44442-solved-help-with-checking-data/#findComment-216187 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.