Page 1 of 1

PHP user registration form

Posted: Fri May 22, 2009 2:49 pm
by tomsace
Hey,
I have managed to get a contact form working on my site, it was a form I downloaded from the net and edited it to my liking.
It has error messages above the form if a field is left blank, and when all is filled in it sends all info to an email. Now I want to use this same form in a user registration form so it inserts data into the mysql database.

Anyway here is how far I have got with the form so far, I have set out all the form's layout and have the error messages working. Now I just cant figure out how to insert the mysql part into it.

Heres my code so far:

Code: Select all

<?php include("templates/bluetop.txt"); ?>
<h1>User Registration</h1>
<center><table><tr><td>
 
 
<font size="2">
            <!-- The form starts here-->
            <?php
                 $error    = '';
                 $username     = '';
                 $password    = '';
                 $email  = '';
                 $day  = '';
                 $month = '';
                 $year = '';
                 $newsletter = '';
 
            if(isset($_POST['send']))
            {
                 $username     = $_POST['username'];
                 $password    = $_POST['password'];
                 $email  = $_POST['email'];
                 $day  = $_POST['day'];
                 $month = $_POST['month'];
                 $year = $_POST['year'];
                 $newsletter = $_POST['newsletter'];
 
                if(trim($username) == '')
                {
                    $error = 'Please select a unique username!<br><br>';
                }
                    else if(trim($password) == '')
                {
                    $error = 'Please enter a password only you will know!<br><br>';
                }
                    else if(trim($email) == '')
                {
                    $error = 'Please enter your email address!<br><br>';
                }
                    else if(!isEmail($email))
                {
                    $error = 'You have entered an invalid email address. Please, try again!<br><br>';
                }
                    else if(trim($day) == '')
                {
                    $error = 'Please enter the day you was born!<br><br>';
                }
                else if(trim($month) == '')
                {
                    $error = 'Please enter the month you was born!<br><br>';
                }
                    else if(trim($year) == '')
                {
                    $error = 'Please enter the year you was born!<br><br>';
                }
 
                require_once('recaptchalib.php');
                $privatekey = "6LfscgYAAAAAAPf_NA5wYd9QB17HK4b9lJLnjNUu";
                $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
 
                if (!$resp->is_valid) {
                  $error = "The verification words you entered were wrong!<br><br>";
                }
 
                if($error == '')
                {
                    if(get_magic_quotes_gpc())
                    {
                        $message = stripslashes($message);
                    }
 
                    // the email will be sent here
                    // make sure to change this to be your email
                    $to      = "admin@zippygame.com";
 
                    // the email subject
                    // '' will appear automatically in the subject.
                    // You can change it as you want
 
                    $subject = $subject;
 
                    // the mail message ( add any additional information if you want )
                    $msg     = "From : $name \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";
 
                    mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
            ?>
 
                  <!-- Message sent! (change the text below as you wish)-->
                    <h1>Message Sent</h1><font size="2">
                    Thankyou for your message, if you are awaiting a reply you should recieve one within 48 hours.<br><br>
                  <!--End Message Sent-->
 
 
            <?php
                }
            }
 
            if(!isset($_POST['send']) || $error != '')
            {
            ?>
<div>
            <!--Error Message-->
            <b><font color="#ff3838"><?=$error;?></b></font></div>
 
            <form  method="post" name="contFrm" id="contFrm" action="">
 
                                <table cellpadding="0" cellspacing="0" border="0" align="center"><tr><td width="100" align="right" valign="top">
                                <label> Username:</label></td><td style="padding-left: 7px;">
                        <input name="username" type="text" class="input" id="username" size="30" value="<?=$username;?>" />
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">
 
                        <label> Password: </label></td><td style="padding-left: 7px;">
                        <input name="password" type="text" class="input" id="password" size="30" value="<?=$password;?>" />
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">
 
                        <label> Email: </label></td><td style="padding-left: 7px;">
                        <input name="email" type="text" class="input" id="email" size="30" value="<?=$email;?>" />
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">
 
                        <label> Date of Birth: </label></td><td style="padding-left: 7px;">
<select name="day" class="input" style="width: 40px;">
<option value="1" <?php if($day == "1") print "selected"; ?>>1</option>
<option value="2" <?php if($day == "2") print "selected"; ?>>2</option>
<option value="3" <?php if($day == "3") print "selected"; ?>>3</option>
<option value="4" <?php if($day == "4") print "selected"; ?>>4</option>
<option value="5" <?php if($day == "5") print "selected"; ?>>5</option>
<option value="6" <?php if($day == "6") print "selected"; ?>>6</option>
<option value="7" <?php if($day == "7") print "selected"; ?>>7</option>
<option value="8" <?php if($day == "8") print "selected"; ?>>8</option>
<option value="9" <?php if($day == "9") print "selected"; ?>>9</option>
<option value="10" <?php if($day == "10") print "selected"; ?>>10</option>
<option value="11" <?php if($day == "11") print "selected"; ?>>11</option>
<option value="12" <?php if($day == "12") print "selected"; ?>>12</option>
<option value="13" <?php if($day == "13") print "selected"; ?>>13</option>
<option value="14" <?php if($day == "14") print "selected"; ?>>14</option>
<option value="15" <?php if($day == "15") print "selected"; ?>>15</option>
<option value="16" <?php if($day == "16") print "selected"; ?>>16</option>
<option value="17" <?php if($day == "17") print "selected"; ?>>17</option>
<option value="18" <?php if($day == "18") print "selected"; ?>>18</option>
<option value="19" <?php if($day == "19") print "selected"; ?>>19</option>
<option value="20" <?php if($day == "20") print "selected"; ?>>20</option>
<option value="21" <?php if($day == "21") print "selected"; ?>>21</option>
<option value="22" <?php if($day == "22") print "selected"; ?>>22</option>
<option value="23" <?php if($day == "23") print "selected"; ?>>23</option>
<option value="24" <?php if($day == "24") print "selected"; ?>>24</option>
<option value="25" <?php if($day == "25") print "selected"; ?>>25</option>
<option value="26" <?php if($day == "26") print "selected"; ?>>26</option>
<option value="27" <?php if($day == "27") print "selected"; ?>>27</option>
<option value="28" <?php if($day == "28") print "selected"; ?>>28</option>
<option value="29" <?php if($day == "29") print "selected"; ?>>29</option>
<option value="30" <?php if($day == "30") print "selected"; ?>>30</option>
<option value="31" <?php if($day == "31") print "selected"; ?>>31</option>
</select>
 
<select name="month" class="input" style="width: 76px;">
<option value="January" <?php if($month == "January") print "selected"; ?>>January</option>
<option value="February" <?php if($month == "February") print "selected"; ?>>February</option>
<option value="March" <?php if($month == "March") print "selected"; ?>>March</option>
<option value="April" <?php if($month == "April") print "selected"; ?>>April</option>
<option value="May" <?php if($month == "May") print "selected"; ?>>May</option>
<option value="June" <?php if($month == "June") print "selected"; ?>>June</option>
<option value="July" <?php if($month == "July") print "selected"; ?>>July</option>
<option value="August" <?php if($month == "August") print "selected"; ?>>August</option>
<option value="September" <?php if($month == "September") print "selected"; ?>>September</option>
<option value="October" <?php if($month == "October") print "selected"; ?>>October</option>
<option value="November" <?php if($month == "November") print "selected"; ?>>November</option>
<option value="December" <?php if($month == "December") print "selected"; ?>>December</option>
</select>
 
<select name="year" class="input" style="width: 55px;">
<?php $year = date("Y"); for ($i = 0; $i <= 100; $i++) {echo "<option value='$year'>$year</option>"; $year--;} ?>
</select>
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">Enter The Words:<br>(with a space)</td>
                                <td style="padding-left: 7px; padding-bottom: 7px;">
 
                <script type="text/javascript">
var RecaptchaOptions = {theme: 'custom', lang: 'en', custom_theme_widget: 'recaptcha_widget'};
</script>
<div id="recaptcha_widget" style="display:none">
<div id="recaptcha_image"><?php require_once('recaptchalib.php');
                $publickey = "6LfscgYAAAAAAM60iGDLMJuIDnIAJyUPi18eiE_z ";
                echo recaptcha_get_html($publickey); ?></div></td></tr><tr><td></td><td style="padding-left: 7px;">
 
<input type="text" class="input" id="recaptcha_response_field" name="recaptcha_response_field" size="28" />&nbsp;&nbsp;
<a href="javascript&#058;Recaptcha.reload()" class="captcha">Reload</a> | <a href="javascript&#058;Recaptcha.showhelp()" class="captcha">Help</a> | 
<a href="http://recaptcha.net/" class="captcha" target="_blank">reCAPTCHA</a>
</div><script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6LfscgYAAAAAAM60iGDLMJuIDnIAJyUPi18eiE_z"></script>
</td></tr><tr><td>
 
                        <!-- Submit Button--></td><td align="center"><br>
                        <input name="send" type="submit" class="button" id="send" value="" />
                        </td></tr></table>
 
            </form>
 
            <?php } ?>
            <!-- END FORM -->
 
     </div> <!-- /contentForm -->
 
</td></tr></table>
<?php include("templates/bluebot.txt"); ?>


Heres my insert code:

Code: Select all

<?php
mysql_query ('ALTER TABLE games AUTO_INCREMENT = 1');
if (isset($_REQUEST['Send'])) {
$sql = "INSERT INTO $users(id, username, password, email, day, month, year, newsletter)
VALUES('','$_POST[username]','$_POST[password]','$_POST[email]','$_POST[day]','$_POST[month]','$_POST[year]','$_POST[newsletter]'')";
I can't manage to get this working right! I have spent a good 4 hours on it now... Any help or pointers will be great!
Thanks alot.

Re: PHP user registration form

Posted: Fri May 22, 2009 3:25 pm
by alpat
If you could perhaps post the error msg it would enable us to find the cause of the error.

From what i can see you currently have no MySQL connection estalished, which will be needed before any attempt to modify the database.

take a look here.....

http://www.php-mysql-tutorial.com/wikis ... abase.aspx

I don't want to pick at the code, however a few notes may point you in the right direction....

The following is not nessasary on each request:

Code: Select all

mysql_query ('ALTER TABLE games AUTO_INCREMENT = 1');
this is altering the database struture on each request, it's not needed....

Also....the following lines are defining new vars in order to check the user input is correct

Code: Select all

                
$username     = $_POST['username'];
$password    = $_POST['password'];
 
etc etc
 
you use trim to check that there is no trailing spaces so you can have clean data to submit to the db....

whats needed is the following.....

Code: Select all

 
$sql = "INSERT INTO $users(id, username, password, email, day, month, year, newsletter)
VALUES('','[b]$username','$password','$email'[/b]....etc
as i said, don't wanna start picking, but also consider arrays for you date/month/year combo, you would be amazed how much of the above code can be removed....

take a look here.....

http://www.w3schools.com/php/php_arrays.asp

hope this helps!

Re: PHP user registration form

Posted: Fri May 22, 2009 7:10 pm
by tomsace
Hey,
Thanks for help, I have tried merging the 2 codes together, but its not gone well at all!!! Try it out... http://www.zippygame.com/user.php?id=register its adding whats wrote without checking... everything everywhere! PLESE REALLY NEED MEGA HELP!

My code is:

Code: Select all

           <?php
                 $error    = '';
                 $username     = '';
                 $password    = '';
                 $email  = '';
                 $day  = '';
                 $month = '';
                 $year = '';
                 $newsletter = '';
 
            mysql_query ('ALTER TABLE users AUTO_INCREMENT = 1');
            if(isset($_POST['send']))
            {
                 $username     = $_POST['username'];
                 $password    = $_POST['password'];
                 $email  = $_POST['email'];
                 $day  = $_POST['day'];
                 $month = $_POST['month'];
                 $year = $_POST['year'];
                 $newsletter = $_POST['newsletter'];
 
            $sql = "INSERT INTO users(id, username, password, email, day, month, year, newsletter) VALUES('', '$username', '$password', '$email', '$day', 
            '$month', '$year', '$newsletter')";
            if($result = mysql_query($sql ,$link)) {
            echo '<center><br>You have successfully registered an account. We have sent you an email, please click the link provided to activate your 
            account.<br>';
            } else {
            echo "ERROR: ".mysql_error();
            } } else {
 
                if(trim($username) == '')
                {
                    $error = 'Please select a unique username!<br><br>';
                }
                    else if(trim($password) == '')
                {
                    $error = 'Please enter a password only you will know!<br><br>';
                }
                    else if(trim($email) == '')
                {
                    $error = 'Please enter your email address!<br><br>';
                }
                    else if(!isEmail($email))
                {
                    $error = 'You have entered an invalid email address. Please, try again!<br><br>';
                }
                    else if(trim($day) == '')
                {
                    $error = 'Please enter the day you was born!<br><br>';
                }
                else if(trim($month) == '')
                {
                    $error = 'Please enter the month you was born!<br><br>';
                }
                    else if(trim($year) == '')
                {
                    $error = 'Please enter the year you was born!<br><br>';
                }
 
                require_once('recaptchalib.php');
                $privatekey = "6LfscgYAAAAAAPf_NA5wYd9QB17HK4b9lJLnjNUu";
                $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
 
                if (!$resp->is_valid) {
                  $error = "The verification words you entered were wrong!<br><br>";
                }
 
                if($error == '')
                {
                    if(get_magic_quotes_gpc())
                    {
                        $message = stripslashes($message);
                    }
 
                    // the email will be sent here
                    // make sure to change this to be your email
                    $to      = "admin@zippygame.com";
 
                    // the email subject
                    // '' will appear automatically in the subject.
                    // You can change it as you want
 
                    $subject = $subject;
 
                    // the mail message ( add any additional information if you want )
                    $msg     = "From : $name \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";
 
                    mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
            ?>
 
                  <!-- Message sent! (change the text below as you wish)-->
                    <h1>Message Sent</h1><font size="2">
                    Thankyou for your message, if you are awaiting a reply you should recieve one within 48 hours.<br><br>
                  <!--End Message Sent-->
 
 
            <?php
                }
            }
 
            if(!isset($_POST['send']) || $error != '')
            {
            ?>
<div>
            <!--Error Message-->
            <b><font color="#ff3838"><?=$error;?></b></font></div>
 
            <form  method="post" name="contFrm" id="contFrm" action="">
 
                                <table cellpadding="0" cellspacing="0" border="0" align="center"><tr><td width="100" align="right" valign="top">
                                <label> Username:</label></td><td style="padding-left: 7px;">
                        <input name="username" type="text" class="input" id="username" size="30" value="<?=$username;?>" />
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">
 
                        <label> Password: </label></td><td style="padding-left: 7px;">
                        <input name="password" type="text" class="input" id="password" size="30" value="<?=$password;?>" />
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">
 
                        <label> Email: </label></td><td style="padding-left: 7px;">
                        <input name="email" type="text" class="input" id="email" size="30" value="<?=$email;?>" />
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">
 
                        <label> Date of Birth: </label></td><td style="padding-left: 7px;">
<select name="day" class="input" style="width: 40px;">
<option value="1" <?php if($day == "1") print "selected"; ?>>1</option>
<option value="2" <?php if($day == "2") print "selected"; ?>>2</option>
<option value="3" <?php if($day == "3") print "selected"; ?>>3</option>
<option value="4" <?php if($day == "4") print "selected"; ?>>4</option>
<option value="5" <?php if($day == "5") print "selected"; ?>>5</option>
<option value="6" <?php if($day == "6") print "selected"; ?>>6</option>
<option value="7" <?php if($day == "7") print "selected"; ?>>7</option>
<option value="8" <?php if($day == "8") print "selected"; ?>>8</option>
<option value="9" <?php if($day == "9") print "selected"; ?>>9</option>
<option value="10" <?php if($day == "10") print "selected"; ?>>10</option>
<option value="11" <?php if($day == "11") print "selected"; ?>>11</option>
<option value="12" <?php if($day == "12") print "selected"; ?>>12</option>
<option value="13" <?php if($day == "13") print "selected"; ?>>13</option>
<option value="14" <?php if($day == "14") print "selected"; ?>>14</option>
<option value="15" <?php if($day == "15") print "selected"; ?>>15</option>
<option value="16" <?php if($day == "16") print "selected"; ?>>16</option>
<option value="17" <?php if($day == "17") print "selected"; ?>>17</option>
<option value="18" <?php if($day == "18") print "selected"; ?>>18</option>
<option value="19" <?php if($day == "19") print "selected"; ?>>19</option>
<option value="20" <?php if($day == "20") print "selected"; ?>>20</option>
<option value="21" <?php if($day == "21") print "selected"; ?>>21</option>
<option value="22" <?php if($day == "22") print "selected"; ?>>22</option>
<option value="23" <?php if($day == "23") print "selected"; ?>>23</option>
<option value="24" <?php if($day == "24") print "selected"; ?>>24</option>
<option value="25" <?php if($day == "25") print "selected"; ?>>25</option>
<option value="26" <?php if($day == "26") print "selected"; ?>>26</option>
<option value="27" <?php if($day == "27") print "selected"; ?>>27</option>
<option value="28" <?php if($day == "28") print "selected"; ?>>28</option>
<option value="29" <?php if($day == "29") print "selected"; ?>>29</option>
<option value="30" <?php if($day == "30") print "selected"; ?>>30</option>
<option value="31" <?php if($day == "31") print "selected"; ?>>31</option>
</select>
 
<select name="month" class="input" style="width: 76px;">
<option value="January" <?php if($month == "January") print "selected"; ?>>January</option>
<option value="February" <?php if($month == "February") print "selected"; ?>>February</option>
<option value="March" <?php if($month == "March") print "selected"; ?>>March</option>
<option value="April" <?php if($month == "April") print "selected"; ?>>April</option>
<option value="May" <?php if($month == "May") print "selected"; ?>>May</option>
<option value="June" <?php if($month == "June") print "selected"; ?>>June</option>
<option value="July" <?php if($month == "July") print "selected"; ?>>July</option>
<option value="August" <?php if($month == "August") print "selected"; ?>>August</option>
<option value="September" <?php if($month == "September") print "selected"; ?>>September</option>
<option value="October" <?php if($month == "October") print "selected"; ?>>October</option>
<option value="November" <?php if($month == "November") print "selected"; ?>>November</option>
<option value="December" <?php if($month == "December") print "selected"; ?>>December</option>
</select>
 
<select name="year" class="input" style="width: 55px;">
<?php $year = date("Y"); for ($i = 0; $i <= 100; $i++) {echo "<option value='$year'>$year</option>"; $year--;} ?>
</select>
                        </td></tr><tr><td height="5"></td></tr><tr><td align="right" valign="top">Enter The Words:<br>(with a space)</td>
                                <td style="padding-left: 7px; padding-bottom: 7px;">
 
                <script type="text/javascript">
var RecaptchaOptions = {theme: 'custom', lang: 'en', custom_theme_widget: 'recaptcha_widget'};
</script>
<div id="recaptcha_widget" style="display:none">
<div id="recaptcha_image"><?php require_once('recaptchalib.php');
                $publickey = "6LfscgYAAAAAAM60iGDLMJuIDnIAJyUPi18eiE_z ";
                echo recaptcha_get_html($publickey); ?></div></td></tr><tr><td></td><td style="padding-left: 7px;">
 
<input type="text" class="input" id="recaptcha_response_field" name="recaptcha_response_field" size="28" />&nbsp;&nbsp;
<a href="javascript&#058;Recaptcha.reload()" class="captcha">Reload</a> | <a href="javascript&#058;Recaptcha.showhelp()" class="captcha">Help</a> |
<a href="http://recaptcha.net/" class="captcha" target="_blank">reCAPTCHA</a>
</div><script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6LfscgYAAAAAAM60iGDLMJuIDnIAJyUPi18eiE_z"></script>
</td></tr><tr><td>
 
                        <!-- Submit Button--></td><td align="center"><br>
                        <input name="send" type="submit" class="button" id="send" value="" />
                        </td></tr></table>
 
            </form>
 
            <?php } ?>