Grow your CSS skills. Land your dream job.

Email Activation For Registration Forms

  • # July 12, 2010 at 5:35 pm

    hi guys
    i have this script to allow user registration with activation link. all data goes in a database.

    the script works fine but when i add an user in my DB i see two records, the first one with the right data and the second one with a blank record.
    if i don’t delete this record i can’t add more users, in my web page compare Error: Duplicate entry ” for key 2

    this is the code that going me crazy

    Code:
    < ?php
    $username = "myname";
    $password = "mypass";
    $host = "localhost";
    $database = "my_db";

    $db = mysql_connect($host, $username, $password) or die("Errore durante la connessione al database");
    mysql_select_db($database, $db) or die("Errore durante la selezione del database");

    if ($_POST['form_submitted'] == '1') {

    ## Form was submitted,the user is registering!

    } else{

    ## No value found, user must be activating their account!

    }

    $activationKey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
    $username = mysql_real_escape_string($_POST[username]);
    $password = mysql_real_escape_string($_POST[password]);

    $email = mysql_real_escape_string($_POST[email]);

    $sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";
    ;

    if (!mysql_query($sql))

    {

    die('Error: ' . mysql_error());

    }

    echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";

    ##Send activation Email

    $to = $_POST[email];

    $subject = " YOURWEBSITE.com Registration";

    $message = "Welcome to our website!rr You, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:rhttp://yourwebsite.com/registrant/verify.php?$activationKeyrr If this is an error, ignore this email and you will be removed from our mailing list.rrRegards, YOURWEBSITE.com Team";

    $headers = 'From: noreply@ YOURWEBSITE.com' . "rn" .

    'Reply-To: noreply@ YOURWEBSITE.com' . "rn" .

    'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    ##User isn't registering, check verify code and change activation code to null, status to activated on success

    $queryString = $_SERVER['QUERY_STRING'];

    $query = "SELECT * FROM users";

    $result = mysql_query($query) or die(mysql_error());

    while($row = mysql_fetch_array($result)){

    if ($queryString == $row["activationkey"]){

    echo "Congratulations!" . $row["username"] . " is now the proud new owner of a YOURWEBSITE.com account.";

    ##$sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";

    $sql="UPDATE users SET activationkey = 'Done-$row[id]', status='activated' WHERE (id = $row[id])";

    if (!mysql_query($sql))

    {

    die('Error: ' . mysql_error());

    }

    }

    }

    ?>

    thanks for your help guys

    # July 13, 2010 at 11:38 am

    i’ve tried every solution on comments but i can’t still figure out

    # July 14, 2010 at 2:36 am

    I have just give you the sample coding format, It will be very useful for you

    Code for Textbox
    <input type="text" value="" name="sample" size="15"/>

    Code for Posting the value
    $sample = $_POST;

    Code for Inserting the values in the database
    $sql = "INSERT INTO profile(image)" .
    "VALUES(‘".$image."’) ";
    mysql_db_query($db, $sql);

    Please assign all the values like this, now the values will insert the database only one time… :D

    # July 15, 2010 at 11:36 am

    i’ve opted to build it from myself, i want learn php!

    now i can’t understand why i can’t insert data into database, mysql_query function give me always error.

    so this is my form

    Code:
    <form class="form" action="verify.php" method="post">
    <p>
    <label>First name
    <input type="text" name="firstname" id="firstname" />
    </label>
    </p>
    <p>
    <label>Last name
    <input type="text" name="lastname" id="lastname" />
    </label>
    </p>
    <p>
    <label>Job Title
    <input type="text" name="jobtitle" id="jobtitle" />
    </label>
    </p>
    <p>
    <label>Organization
    <input name="organization" type="text" id="organization" cols="45" rows="5"></textarea>
    </label>
    </p>
    <p>
    <label>Describe your organization in 100 words<br />
    <textarea name="describeorg" id="describeorg" cols="30" rows="5"></textarea>
    </label>
    </p>
    <p>
    <label>Who to meet at the conference<br />
    <textarea name="meet" id="meet" cols="30" rows="5"></textarea>
    </label>
    </p>
    <p>
    <label>Email
    <input type="text" name="email" id="email" />
    </label>
    </p>
    <table width="497">
    <tr>
    <td width="162">&nbsp;</td>
    <td width="299">accept to be audio/video recorded<br />
    and participation to the event made public</td>
    <td width="20" align="center"><input type="checkbox" name="accept" id="accept" /></td>
    </tr>
    </table>
    </p>
    <table width="496">
    <tr>
    <td width="270" rowspan="2" align="left">&nbsp;</td>
    <td width="161" rowspan="2" align="left">need special assistance (e.g. wheelchair accessible)</td>
    <td width="49" align="right">no <input type="radio" name="noassistance" value="no" id="RadioGroup1_0" /></td>
    </tr>
    <tr>
    <td align="right">yes <input type="radio" name="yesassistant" value="yes" id="RadioGroup1_1" /></td>
    </tr>
    </table>
    <p>
    <label>
    <input type="submit" name="submit" id="submit" value="Register" />
    </label>
    </p>
    </form>

    and my verify.php code

    Code:
    <?php
    $firstname=$_POST[’firstname’];
    $lastname=$_POST[’lastname’];
    $jobtitle=$_POST[’jobtitle’];
    $organization=$_POST[’organization’];
    $describeorg=$_POST[’describeorg’];
    $meet=$_POST[’meet’];
    $email=$_POST[’email’];
    $accept=$_POST[’accept’];
    $noassistance=$_POST[’noassistance’];
    $yesassistance=$_POST[’yesassistance’];

    $username = "user";
    $password = "pass";
    $host = "localhost";
    $database = "my_db";

    $db = mysql_connect($host, $username, $password) or die("Errore durante la connessione al database");
    mysql_select_db($database, $db) or die("Errore durante la selezione del database");

    $query = "INSERT INTO participants ( ID, firstname, lastname, jobtitle, organization, describeorg, meet, email, accept, noassistance, yesassistance) VALUES (‘NULL’, ‘.$firstname’, ‘.$lastname’, ‘.$jobtitle’, ‘.$organization’, ‘.$describeorg’, ‘.$meet’, ‘.$email’, ‘.$accept’, ‘.$noassistance’, ‘.$yesassistance’)";

    mysql_query($query, $db) or die (‘error’);

    ?>

    and at least my query for table

    Code:

    – MySQL 4.1.22
    – Thu, 15 Jul 2010 17:33:50 +0200

    CREATE TABLE `participants` (
    `ID` int(10) not null default ’0′,
    `firstname` varchar(20),
    `lastname` varchar(20),
    `jobtitle` varchar(20),
    `organization` varchar(20),
    `describeorg` varchar(100),
    `meet` varchar(100),
    `email` varchar(20),
    `noassistance` char(2),
    `yesassistance` char(2),
    PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

    – [Table `participants` is empty]

    # July 16, 2010 at 5:19 am

    Instead of returning ‘error’ when it fails, check mysql_error() – that way you can actually see why it’s failing.

    # July 16, 2010 at 9:31 am

    ok now i know a new command!

    i wrote mysql_query($query) or die (mysql_error())

    and i get this error, Unknown column ‘ID’ in ‘field list’

    where am i wrong?

    Rob
    # July 16, 2010 at 9:34 am

    you are probably calling your ID column in upper case and the col name is actually in lower case. It is something like that. Else the ID col dosen’t exist at all :D

    # July 18, 2010 at 6:49 am
    "Robskiwarrior" wrote:
    you are probably calling your ID column in upper case and the col name is actually in lower case. It is something like that. Else the ID col dosen’t exist at all :D

    you got it dude! :D

    now my form with activation email works pretty well.

    now i’ve added with the help of dreamweaver cs4 the spry validation field and it works fine too.

    my last question is, could a form with required field and activation email avoid spam?

    i must to embed a captcha field?

    thanks

    # July 26, 2010 at 4:29 am

    i would like embed a captcha sistem. it works but the required fields are like not required, so if complete only the captcha the form sent data with blank field.
    I tried two different (reCAPTCHA google and another) but both have the same result.

    solution at this point that I could invent?

    # August 10, 2012 at 12:18 pm

    The form works perfectly! I implemented this into a popup. However, once the person hits submit, they get the generic text “An email has been sent to THEIR EMAIL with an activation key. Please check your mail to complete registration.” I created a nice looking success page to replace this, but I don’t know how to code it in the PHP. Help!

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".