Form Checking? [help]

Status
Not open for further replies.

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
I have a E-Mail subscription form on my site, and I'd like to know how to keep people from entering the same E-Mail more than once, or from just entering any other E-Mail. (so they can only enter once)
How can I do this? (In php?)
 

Alam

shietttt
Jul 3, 2011
433
166
You should have variables on top regarding you should also have a $query variable that will request data to your database so just do mysql_num_rows($query) if they query is used again the data will not insert to database.
 

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
You should have variables on top regarding you should also have a $query variable that will request data to your database so just do mysql_num_rows($query) if they query is used again the data will not insert to database.
could you help me with that? I know nothing about SQL and so little aboot php.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Well since you're not storing the email addresses and just having them sent to you, there is no possible way of checking against previous records of email addresses to see if it already exists.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Your best off storing them in a mysql database, and once a person clicks submit (on the form) you need to make it to check if an entry already exists.
If it does exist then it will return them with an error, if it doesnt then their email will be inserted into the database
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
You can set the database up yourself, but I'll start you off on a little checking system.

It should work, I coded it in the post box here. As I said, create the database yourself and change the MySQL details and table/column details.

PHP:
<?php
mysql_connect( "localhost", "user", "pass" ) or die( mysql_error() );
mysql_select_db( "dbname" ) or die( mysql_error() );
 
if ( isset( $_POST["submit"] ) )
{
    $email = strip_tags( $_POST["email"] );
    $error = array();
 
    if ( empty( $error ) ) $error[] = 'Please enter an email address.';
    if ( !filter_var( $email, FILTER_VALIDATE_EMAIL ) ) $error[] = 'Please enter a valid email address.';
    if ( mysql_num_rows( mysql_query( "SELECT * FROM `EmailTable` WHERE `EmailAddress` = '" . mysql_real_escape_string( addslashes( $email ) ) . "'" ) ) > 0 ) $error[] = 'This email address has already been used.';
 
    if ( !empty( $error ) )
    {
        echo '<ul>';
        foreach ( $error as $v ) { echo '<li>' . $v . '</li>'; }
        echo '</ul>';
    }
    else
    {
        @mysql_query( "INSERT INTO `EmailTable` (`EmailAddress`, `IPAddress`) VALUES ('" . $email . "', '" . $_SERVER['REMOTE_ADDR'] . "')" );
        mail( "[email protected]", "subject", "message", "From: {$email}" );
    }
}
else
{
    echo '<form method="post" action="' . $_SERVER["PHP_SELF"] . '">
    <label for="email">Email Address:</label> <input type="text" name="email" id="email" value="" /><br />
    <input type="submit" name="submit" value="Go" />
    </form>';
}
?>
 

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
You can set the database up yourself, but I'll start you off on a little checking system.

It should work, I coded it in the post box here. As I said, create the database yourself and change the MySQL details and table/column details.

PHP:
<?php
mysql_connect( "localhost", "user", "pass" ) or die( mysql_error() );
mysql_select_db( "dbname" ) or die( mysql_error() );
 
if ( isset( $_POST["submit"] ) )
{
    $email = strip_tags( $_POST["email"] );
    $error = array();
 
    if ( empty( $error ) ) $error[] = 'Please enter an email address.';
    if ( !filter_var( $email, FILTER_VALIDATE_EMAIL ) ) $error[] = 'Please enter a valid email address.';
    if ( mysql_num_rows( mysql_query( "SELECT * FROM `EmailTable` WHERE `EmailAddress` = '" . mysql_real_escape_string( addslashes( $email ) ) . "'" ) ) > 0 ) $error[] = 'This email address has already been used.';
 
    if ( !empty( $error ) )
    {
        echo '<ul>';
        foreach ( $error as $v ) { echo '<li>' . $v . '</li>'; }
        echo '</ul>';
    }
    else
    {
        mail( "[email protected]", "subject", "message", "From: {$email}" );
    }
}
else
{
    echo '<form method="post" action="' . $_SERVER["PHP_SELF"] . '">
    <label for="email">Email Address:</label> <input type="text" name="email" id="email" value="" /><br />
    <input type="submit" name="submit" value="Go" />
    </form>';
}
?>
Thanks!
 
Status
Not open for further replies.

Users who are viewing this thread

Top