Jump to content

email form verification


enhanced08

Recommended Posts

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

Link to comment
Share on other sites

        $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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.