enhanced08 Posted May 17, 2010 Share Posted May 17, 2010 Ive been having this problem for awhile. Basically what I want to do is a mailing list. I want to have a user enter an email address and have it added to a database. i want a script that will check for an input (to make sure they dont hit submit before they type anything), check to make sure the data is an email address, and query the database to see if it is already there. I was able to get the data verification and email verification to work but I can not get it to check the database. I have tried and tried, Im no pro at PHP by any means and am learning as I go. Can someone tell me what Im doing wrong? I thought this was going to be simple. Here is my code: <? $db_user="**********"; $db_pass="**********"; $database="**********"; $host="**************"; $sent_email=$_POST['email']; $sent_email=strtolower($sent_email); $error='';//initialize $error to blank mysql_connect($host, $db_user, $db_pass); @mysql_select_db($database) or die( "Unable to select database"); if(trim($sent_email)==''){ $error="An email address is required!<br />"; } else { $query="SELECT * FROM mailing_list WHERE email=$sent_email"; $result=mysql_query($query, 0) or die(mysql_error()); if($result==$sent_email){ $error="Your email address is already in our database.<br />"; } else { if(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $_POST['email'])) { $error="<p>The e-mail you entered was not in the proper format!<br><br> Try again.<br> <form action=mailing_list_add.php method=post accept-charset=utf-8> <table border=0 cellspacing=2 cellpadding=0> <tr><th>Email</th><td><input type=text name=email class=text></td></tr> </td></tr> <tr><td class=submission colspan=2><input type=submit name=s value=Submit></td></tr> </table> </form>"; } } } if($error==''){ { $query="INSERT INTO mailing_list VALUES ('','$sent_email')"; mysql_query($query); mysql_close(); } //echo "<script type=\"text/javascript\"> window.location = \"thankyou2.htm\"</script>"; } else{ echo "<span style=color:red>$error</span>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/202097-email-form-verification/ Share on other sites More sharing options...
.Stealth Posted May 17, 2010 Share Posted May 17, 2010 $query="SELECT * FROM mailing_list WHERE email=$sent_email"; $sql=mysql_query($query, 0) or die(mysql_error()); $result = mysql_num_rows($sql); if($result >= 1){//Email already exists $error="Your email address is already in our database.<br />"; } That's how you check against other records. If you don't understand anything in the above let me know and I'll explain. Quote Link to comment https://forums.phpfreaks.com/topic/202097-email-form-verification/#findComment-1059797 Share on other sites More sharing options...
trq Posted May 17, 2010 Share Posted May 17, 2010 $result holds a database result resource so it will never be equal to $sent_email. Instead of.... if($result==$sent_email){ try.... if (mysql_num_rows($result)) { Quote Link to comment https://forums.phpfreaks.com/topic/202097-email-form-verification/#findComment-1059798 Share on other sites More sharing options...
enhanced08 Posted May 17, 2010 Author Share Posted May 17, 2010 Ok, that does make sense and is much easier than what I was trying to do. One question I do have, it may not be directly related, however, is what exactly is/does the "0" do here: $sql=mysql_query($query, 0); I know I have that in my original code but thats because someone else told me I need it, Im just not sure what it is for. Quote Link to comment https://forums.phpfreaks.com/topic/202097-email-form-verification/#findComment-1059800 Share on other sites More sharing options...
trq Posted May 17, 2010 Share Posted May 17, 2010 Never even noticed it. You best remove it, it does nothing and I would have thought would be giving you an error. The second arument to mysql_query is an optional connections resource. Optional meaning, not required. Whoever told you to put a 0 there was incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/202097-email-form-verification/#findComment-1059804 Share on other sites More sharing options...
enhanced08 Posted May 17, 2010 Author Share Posted May 17, 2010 Alright, I will remove it. Thank you both very much, Im sure Ill be back asking more questions in the future. Quote Link to comment https://forums.phpfreaks.com/topic/202097-email-form-verification/#findComment-1059805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.