mroberts46 Posted June 8, 2012 Share Posted June 8, 2012 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="[email protected]" 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/ Share on other sites More sharing options...
Barand Posted June 8, 2012 Share Posted June 8, 2012 does it help if you use realpath($dest) when creating the file? Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352107 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 does it help if you use realpath($dest) when creating the file? I don't know. I'm still new to php. I am unfamiliar with that function Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352110 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352111 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352112 Share on other sites More sharing options...
Barand Posted June 8, 2012 Share Posted June 8, 2012 does it help if you use realpath($dest) when creating the file? Sorry, had two posts open and replied to the wrong one! Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352119 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 does it help if you use realpath($dest) when creating the file? Sorry, had two posts open and replied to the wrong one! Oh... Okay Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352122 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352127 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352130 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352134 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 add this to the top of the page and see what errors you get: ini_set('display_errors',1); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352135 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352138 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 ok, give me your database table structure for that members table and the script as it stands now and i will run it through my localhost Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352148 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 [*]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="[email protected]" 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352150 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352156 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 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="[email protected]" 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> Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352163 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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="[email protected]" 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? Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352180 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352184 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352192 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 post the completed script on here Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352195 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352199 Share on other sites More sharing options...
gristoi Posted June 8, 2012 Share Posted June 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352211 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 Will do... I'll let you know if I find it...sorry...when I find it Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352216 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 That didn't take long at all. For some reason, I'm not connecting to my db. This is where it died: if(!$connect){ $response[] = mysql_error(); die('i am here!!!!!'); return $response; } Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352219 Share on other sites More sharing options...
mroberts46 Posted June 8, 2012 Author Share Posted June 8, 2012 Finally.... It WORKS!!!! Yea!!! Quote Link to comment https://forums.phpfreaks.com/topic/263851-registration-form/#findComment-1352259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.