treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Email Activation For Registration Forms

  • 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
    <?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!\r\r 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?$activationKey\r\r If this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team\";

    $headers = 'From: noreply@ YOURWEBSITE.com' . \"\r\n\" .

    'Reply-To: noreply@ YOURWEBSITE.com' . \"\r\n\" .

    '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
  • i've tried every solution on comments but i can't still figure out
  • 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['sample'];

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


    and my verify.php code
    &lt;?php
    $firstname=$_POST&#91;'firstname'&#93;;
    $lastname=$_POST&#91;'lastname'&#93;;
    $jobtitle=$_POST&#91;'jobtitle'&#93;;
    $organization=$_POST&#91;'organization'&#93;;
    $describeorg=$_POST&#91;'describeorg'&#93;;
    $meet=$_POST&#91;'meet'&#93;;
    $email=$_POST&#91;'email'&#93;;
    $accept=$_POST&#91;'accept'&#93;;
    $noassistance=$_POST&#91;'noassistance'&#93;;
    $yesassistance=$_POST&#91;'yesassistance'&#93;;


    $username = &quot;user&quot;;
    $password = &quot;pass&quot;;
    $host = &quot;localhost&quot;;
    $database = &quot;my_db&quot;;

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

    $query = &quot;INSERT INTO participants ( ID, firstname, lastname, jobtitle, organization, describeorg, meet, email, accept, noassistance, yesassistance) VALUES ('NULL', '&#46;$firstname', '&#46;$lastname', '&#46;$jobtitle', '&#46;$organization', '&#46;$describeorg', '&#46;$meet', '&#46;$email', '&#46;$accept', '&#46;$noassistance', '&#46;$yesassistance')&quot;;

    mysql_query($query, $db) or die ('error');

    ?&gt;


    and at least my query for table
    --
    -- MySQL 4&#46;1&#46;22
    -- Thu, 15 Jul 2010 17&#58;33&#58;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;

    -- &#91;Table `participants` is empty&#93;
  • Instead of returning 'error' when it fails, check mysql_error() - that way you can actually see why it's failing.
  • 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?
  • 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
  • "Robskiwarrior" said:
    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
  • 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?
  • 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!