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