Jump to content

Registration Form


mroberts46

Recommended Posts

I've been working all through the night trying to get this registration form to insert everything into my db withough one successful attempt. I'm getting frustrated because I have to have this site ready for the organization within the next few hours. Please, can someone take a look at this code and tell me what's going wrong and how I might be able to fix it?

 

<?php
session_start();

function register(){
$connect = mysql_connect("localhost", "username", "password");
if(!$connect){
	$response = mysql_error();
}

$select_db = mysql_select_db("db_name", $connect);
if(!$select_db){
	$response = mysql_error();
}

$first =  mysql_real_escape_string ($_POST['first']);
$last =  mysql_real_escape_string ($_POST['last']);
$email = mysql_real_escape_string ($_POST['email']);
$user =  mysql_real_escape_string ($_POST['username']);
$pass =  mysql_real_escape_string ($_POST['password']);
$conf_pass = mysql_real_escape_string ($_POST['conf_pass']);
$ip = $_SERVER["REMOTE_ADDR"];

if(empty($username)){
	$response = "Please enter your username!";
}
if(empty($password)){
	$response = "Please enter your password!<br>";
}
if(empty($conf_pass)){
	$response = "Please confirm your password!<br>";
}
if(empty($email)){
	$response = "Please enter your email!";
}

$user_check = mysql_query("SELECT username FROM members WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

$email_check = mysql_query("SELECT email FROM members WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

if($do_user_check > 0){
	$response = "Username is already in use!";
}

if($do_email_check > 0){
	$response = "Email is already in use!";
}

if($password != $conf_pass){
	$response = "Passwords don't match!";
}

$insert = mysql_query("INSERT INTO members (`id`, `first`, `last`, `username`, `password`, `email`, `regIP`) VALUES (NULL , '$first', '$last', '$email' '$user', md5('$pass'), '$ip')"); 
if(!$insert){
	$response = "There's a little problem: ".mysql_error();
} else { $response = "Congrats " . $username . "! You are now registered"; }

}
?>

<form action="" id="login" method="post">
               <h1>Register</h1>
               <fieldset id="inputs">
               	<input autofocus id="first" name="first" placeholder="First Name" required type="text">
                    <input id="last" name="last" placeholder="Last Name" required type="text">
                    <input id="email" name="email" placeholder="someone@example.com" required type="email">
                    <input id="username" name="username" placeholder="Username" required type="text">
                    <input id="password" name="password" placeholder="Password" required type="password">
                    <input id="password" name="conf_pass" placeholder="Confirm Password" required type="password">
               </fieldset>
               <fieldset id="actions">
                    <input id="submit" type="submit" value="Register">
                    <a href="">Forgot your password?</a><a href="login.php">Log In</a>
               </fieldset>
          </form>
          <?php if(isset($response)) echo "<p class='alert'>" . $response . "</p>"; ?>

 

Link to comment
Share on other sites

I was following a tutorial I found on the web but I can't get any of them to work for my application. I've tried four different tuts and they all have the same result -- no result. I'm supposed to be showing this to the organization that it is for around lunch time. I was hoping to catch a little sleep before then as well but I doubt that's going to happen today.

Link to comment
Share on other sites

your missing a comma between these two variables:

, '$email' '$user',

should be

, '$email' , '$user',

also you are trying to build up an error response, ie:

if(empty($email)){
	$response = "Please enter your email!";
}

 

but you are not using the response variable correctly. just before you do the insert query you should be checking if response has a value, and if it does then dont execute the insert

Link to comment
Share on other sites

also you are trying to build up an error response, ie:

if(empty($email)){
	$response = "Please enter your email!";
}

 

but you are not using the response variable correctly. just before you do the insert query you should be checking if response has a value, and if it does then dont execute the insert

 

Would you be able to give an example of what you mean? Like, I know there's another if statement that can be made but I don't know how to structure it (been awake for 32 straight hours now). I also know that iwould be better to do it with sessions but every time I've tried to use sessions, i've had nothing but problems.

Link to comment
Share on other sites

ok, heres the revised function:

<?php
function register(){
$response = array();
$connect = mysql_connect("localhost", "username", "password");
if(!$connect){
	$response[] = mysql_error();
	return $response;
}

$select_db = mysql_select_db("db_name", $connect);
if(!$select_db){
	$response[] = mysql_error();
	return $response;
}

$first =  mysql_real_escape_string ($_POST['first']);
$last =  mysql_real_escape_string ($_POST['last']);
$email = mysql_real_escape_string ($_POST['email']);
$user =  mysql_real_escape_string ($_POST['username']);
$pass =  mysql_real_escape_string ($_POST['password']);
$conf_pass = mysql_real_escape_string ($_POST['conf_pass']);
$ip = $_SERVER["REMOTE_ADDR"];

if(empty($username)){
	$response[] = "Please enter your username!";
}
if(empty($password)){
	$response[] = "Please enter your password!<br>";
}
if(empty($conf_pass)){
	$response = "Please confirm your password!<br>";
}
if(empty($email)){
	$response[] = "Please enter your email!";
}

$user_check = mysql_query("SELECT username FROM members WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

$email_check = mysql_query("SELECT email FROM members WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

if($do_user_check > 0){
	$response[] = "Username is already in use!";
}

if($do_email_check > 0){
	$response[] = "Email is already in use!";
}

if($password != $conf_pass){
	$response[] = "Passwords don't match!";
}

if(count($response) > 0 ){
	return $response;
}else
{
$insert = mysql_query("INSERT INTO members (`id`, `first`, `last`, `username`, `password`, `email`, `regIP`) VALUES (NULL , '$first', '$last', '$email', '$user', md5('$pass'), '$ip')"); 
if(!$insert){
	$response[] = "There's a little problem: ".mysql_error();
} else { $response[] = "Congrats " . $username . "! You are now registered"; }
return $response;
}



}

 

how exactly are you executing this function, i cant see anyway for you to passs the data to the function. is the form on the same page as the function, and if it is you will need something like this at the top of the page:

if($_POST['Register']){
	$response = register();
}

otherwise the function will never trigger. also the last part of the form will now need to handle an array:

 

<?php if(isset($response)) echo "<p class='alert'>" .

foreach($response as $r){
	print $r.'</br>';
}

. "</p>"; ?>

Link to comment
Share on other sites

how exactly are you executing this function, i cant see anyway for you to passs the data to the function. is the form on the same page as the function, and if it is you will need something like this at the top of the page:

if($_POST['Register']){
	$response = register();
}

otherwise the function will never trigger. also the last part of the form will now need to handle an array:

 

<?php if(isset($response)) echo "<p class='alert'>" .

foreach($response as $r){
	print $r.'</br>';
}

. "</p>"; ?>

 

I applied the revision that you suggested and tested but still no entry was made in the db. the form and the script are on the same page. Any other suggestions?

Link to comment
Share on other sites

add this to the top of the page and see what errors you get:

ini_set('display_errors',1); 
error_reporting(E_ALL);

 

I don't get any errors at the top of the page like I normally would with session errors. The error message underneath the login box where $response is supposed to show only says "Array" but not what the error is. I added name="Register" to the submit button thinking that it would set in $_POST['Register']. Still trying to think of other things that could be problematic.

Link to comment
Share on other sites

[*]id...int(11)...utf8_unicode_ci...not null...auto increment...primary

[*]first...varchar(30)...unicode_ci...not null

[*]last...varchar(50)...unicode_ci...not null

[*]username...varchar(20)...unicode_ci...not null...unique

[*]password...varchar(64)...unicode_ci...not null

[*]email...varchar(250)...unicode_ci...not null

[*]regIP...varchar(20)...unicode_ci...not null

 

[*]id...int(11)...utf8_unicode_ci...not null...auto increment...primary

[*]first...varchar(30)...unicode_ci...not null

[*]last...varchar(50)...unicode_ci...not null

[*]username...varchar(20)...unicode_ci...not null...unique

[*]password...varchar(64)...unicode_ci...not null

[*]email...varchar(250)...unicode_ci...not null

[*]regIP...varchar(20)...unicode_ci...not null

 



<?php
session_start();
ini_set('display_errors',1);
error_reporting(E_ALL);

if($_POST['Register']){
$response = register();
}

function register(){
$response = array();
$connect = mysql_connect("asudst.ipowermysql.com", "asudst", "3l3ph@nt");
if(!$connect){
$response[] = mysql_error();
return $response;
}

$select_db = mysql_select_db("db_name", $connect);
if(!$select_db){
$response[] = mysql_error();
return $response;
}

$first =  mysql_real_escape_string ($_POST['first']);
$last =  mysql_real_escape_string ($_POST['last']);
$email = mysql_real_escape_string ($_POST['email']);
$user =  mysql_real_escape_string ($_POST['username']);
$pass =  mysql_real_escape_string ($_POST['password']);
$conf_pass = mysql_real_escape_string ($_POST['conf_pass']);
$ip = $_SERVER["REMOTE_ADDR"];

if(empty($username)){
$response[] = "Please enter your username!";
}
if(empty($password)){
$response[] = "Please enter your password!<br>";
}
if(empty($conf_pass)){
$response = "Please confirm your password!<br>";
}
if(empty($email)){
$response[] = "Please enter your email!";
}

$user_check = mysql_query("SELECT username FROM members WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

$email_check = mysql_query("SELECT email FROM members WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

if($do_user_check > 0){
$response[] = "Username is already in use!";
}

if($do_email_check > 0){
$response[] = "Email is already in use!";
}

if($password != $conf_pass){
$response[] = "Passwords don't match!";
}

if(count($response) > 0 ){
return $response;
}else
{
$insert = mysql_query("INSERT INTO members (`id`, `first`, `last`, `username`, `password`, `email`, `regIP`) VALUES (NULL , '$first', '$last', '$email', '$user', md5('$pass'), '$ip')");
if(!$insert){
$response[] = "There's a little problem: ".mysql_error();
} else { $response[] = "Congrats " . $username . "! You are now registered"; }
return $response;
}



}
?>

 



<form action="" id="login" name="Register" method="post">
              <h1>Register</h1>
              <fieldset id="inputs">
              <input autofocus id="first" name="first" placeholder="First Name" required type="text">
                    <input id="last" name="last" placeholder="Last Name" required type="text">
                    <input id="email" name="email" placeholder="someone@example.com" required type="email">
                    <input id="username" name="username" placeholder="Username" required type="text">
                    <input id="password" name="password" placeholder="Password" required type="password">
                    <input id="password" name="conf_pass" placeholder="Confirm Password" required type="password">
              </fieldset>
              <fieldset id="actions">
                    <input id="submit" type="submit" name="Register" value="Register">
                    <a href="">Forgot your password?</a><a href="login.php">Log In</a>
              </fieldset>
          </form>
          <?php if(isset($response)) echo "<p class='alert'>" . $response . "</p>"; ?>

 

Link to comment
Share on other sites

And I still have to do something for "Forgot Password". Lucky for me, that functionality isn't what the bosses are looking for today. Today, they are looking for an easy way of entering in all of their users into a db and to see which of my two designs is more aesthetically pleasing. On design 1, I had a working registration form (until someone deleted it) but the login didn't work. On design two, the login works perfectly well but the registration doesn't work. It would have been nice to be able to pull the working registration from design one but such is life.

Link to comment
Share on other sites

form didnt have name for register set, change form to this:

<form action="" id="login" method="post">
               <h1>Register</h1>
               <fieldset id="inputs">
               	<input autofocus id="first" name="first" placeholder="First Name" required type="text"/>
                    <input id="last" name="last" placeholder="Last Name" required type="text"/>
                    <input id="email" name="email" placeholder="someone@example.com" required type="email"/>
                    <input id="username" name="username" placeholder="Username" required type="text"/>
                    <input id="password" name="password" placeholder="Password" required type="password">
                    <input id="password" name="conf_pass" placeholder="Confirm Password" required type="password">
               </fieldset>
               <fieldset id="actions">
                    <input id="submit" type="submit" name="Register" value="Register">
                    <a href="">Forgot your password?</a><a href="login.php">Log In</a>
               </fieldset>
          </form>

Link to comment
Share on other sites

form didnt have name for register set, change form to this:

<form action="" id="login" method="post">
               <h1>Register</h1>
               <fieldset id="inputs">
               	<input autofocus id="first" name="first" placeholder="First Name" required type="text"/>
                    <input id="last" name="last" placeholder="Last Name" required type="text"/>
                    <input id="email" name="email" placeholder="someone@example.com" required type="email"/>
                    <input id="username" name="username" placeholder="Username" required type="text"/>
                    <input id="password" name="password" placeholder="Password" required type="password">
                    <input id="password" name="conf_pass" placeholder="Confirm Password" required type="password">
               </fieldset>
               <fieldset id="actions">
                    <input id="submit" type="submit" name="Register" value="Register">
                    <a href="">Forgot your password?</a><a href="login.php">Log In</a>
               </fieldset>
          </form>

 

So the error was with the form itself?

Link to comment
Share on other sites

yeah, one of them anyway. basically without

name="Register"

being set in the submit button you would not get

$_POST['Register]

when you posted the form. let me know if it works

 

Still not working. I tried the form twice and all I got was Array underneath my form. I went to mySQL Admin to check db and still no new entry. You can check out the form for yourself at http://www.alcornalumnaedst.org/new_site/testing/register.php

Link to comment
Share on other sites

I removed substituted the $connect info for security reasons but this is the full script

 


<?php
session_start();
ini_set('display_errors',1); 
error_reporting(E_ALL);

if($_POST['Register']){
$response = register();
}

function register(){
$response = array();
$connect = mysql_connect("localhost", "username", "password");
if(!$connect){
	$response[] = mysql_error();
	return $response;
}

$select_db = mysql_select_db("db_name", $connect);
if(!$select_db){
	$response[] = mysql_error();
	return $response;
}

$first =  mysql_real_escape_string ($_POST['first']);
$last =  mysql_real_escape_string ($_POST['last']);
$email = mysql_real_escape_string ($_POST['email']);
$user =  mysql_real_escape_string ($_POST['username']);
$pass =  mysql_real_escape_string ($_POST['password']);
$conf_pass = mysql_real_escape_string ($_POST['conf_pass']);
$ip = $_SERVER["REMOTE_ADDR"];

if(empty($username)){
	$response[] = "Please enter your username!";
}
if(empty($password)){
	$response[] = "Please enter your password!<br>";
}
if(empty($conf_pass)){
	$response = "Please confirm your password!<br>";
}
if(empty($email)){
	$response[] = "Please enter your email!";
}

$user_check = mysql_query("SELECT username FROM members WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

$email_check = mysql_query("SELECT email FROM members WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

if($do_user_check > 0){
	$response[] = "Username is already in use!";
}

if($do_email_check > 0){
	$response[] = "Email is already in use!";
}

if($password != $conf_pass){
	$response[] = "Passwords don't match!";
}

if(count($response) > 0 ){
	return $response;
}else
{
$insert = mysql_query("INSERT INTO members (`id`, `first`, `last`, `username`, `password`, `email`, `regIP`) VALUES (NULL , '$first', '$last', '$email', '$user', md5('$pass'), '$ip')"); 
if(!$insert){
	$response[] = "There's a little problem: ".mysql_error();
} else { $response[] = "Congrats " . $username . "! You are now registered"; }
return $response;
}



}
?>

Link to comment
Share on other sites

ok, you are going to need to debug line by line at this point to make sure the script is not dying. easiest, and quickest way, is to add a die statement into your php one line at a time.:

die('i am here!!!!!);

start at the top line of your function and move the die statment down a line each time until you dont se the i am here any more!!!. that will show you where your script is dying

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.