Jump to content

Register Script Not Working?


kirbyweb

Recommended Posts

I watched the php academy videos and I watched the one where you make a register script.Here is the code I made while watching it:

<phpecho "<h1>Register</h1>";$submit = strip_tags($_POST['submit']);$firstname = strip_tags($_POST['firstname']);$username = strip_tags($_POST['username']);$password = strip_tags($_POST['password']));$repeatpassword = strip_tags($_POST['repeatpassword']));$date = date("Y-m-d");if ($submit){if ($firstname&&$username&&$password&&$repeatpassword){$password = md5($password);$repeat = md5($repeatpassword);if ($password==$repeatpassword);{if (strlen($firstname)>25||strlen($username)>25){echo "Max character length for first name or username is 25.";}else{if (strlen($password)>25||strlen(password)<6){echo "Password is only allowed 6-25 characters long.";}{....}}elseecho "Your passwords do not match.";}elseecho "Please fill in all fields.";}?><html><p><form action='register.php' method='POST'><table><tr><td>Your first name:</td><td><input type='text' name='register'></td></tr><tr><td>Choose a username:</td><td><input type='text' name='username'></td></tr><tr><td>Choose a password:</td><td><input type='password' name='password'></td></tr><tr><td>Repeat your password:</td><td><input type='password' name='repeatpassword'></td></tr></table><p><input type='submit' name='submit value='register'></form></html>

What is the problem with this code, here is the link to the video thanks.http://www.youtube.com/user/phpacademy#p/s...h/0/IUCEEh-ASH8this is only the 3rd tutorial on this, but this is where I got stuck.

Link to comment
Share on other sites

Can you describe the problems you are having? What happens when you display the document, and then when you give it good or bad inputs?

Link to comment
Share on other sites

<php

That's not the correct way to open PHP code. Your if/else structure is also wrong, I don't know what you're trying to do there. You have opening brackets without closing brackets, else blocks attached to other else blocks, etc. It would probably help you to indent your code so that it's easier to see the structure.

Link to comment
Share on other sites

<php

That's not the correct way to open PHP code. Your if/else structure is also wrong, I don't know what you're trying to do there. You have opening brackets without closing brackets, else blocks attached to other else blocks, etc. It would probably help you to indent your code so that it's easier to see the structure.

LOL WOW!!! how did I miss that I missed opened it!!!!!!!! Thanks.Also I am just doing what the video says am I doing it right?
Link to comment
Share on other sites

That's a really weird way of checking all your conditions...it's longwinded and extremely deeply embedded. You don't want to be echoing before you've started your HTML output. You also don't want to be using such a skeletal HTML page - even if they're empty, have head and title tags. Don't have empty paragraphs dotted around the place - or worse, paragraphs with tables in. My suggested way of creating the error messages is to declare an empty variable, such as $display = ''; at the start and then append any error messages to it as you go:if(strlen($password) < 8) {$display .= "The password must be at least 8 characters. \n";}Echo $display in a paragraph at the start of the form:<?php if($display != '') { echo $display; } ?>Check all relevant variables at once:if ($submit && $firstname && $username && $password && $repeatpassword)instead of having that as two ifs.The way I prefer to do it is to exit the script as I find faults:if(!$submit) {deal with it}if(!($username && $password && $repeatPassword && $firstname)) {$display .= "Please fill in all fields. \n";}That way you don't end up with complex embedding of conditions. Try some of those suggestions out and let me know how you get on.

Link to comment
Share on other sites

The code has several errors in it. The PHP will not execute because the code is not correctly formed. The if/else structure is not syntactically valid, so either you didn't understand the video or whoever made the video is uninformed. Just count the brackets - you have 7 opening brackets and 6 closing brackets, obviously that's not going to be correct. And like I said, you have else conditions attached to other else conditions. This is not a valid structure:

else{}else{}

The only thing an else block can be attached to is an if block. And just to point out, this is also not valid PHP code:

{....}

Link to comment
Share on other sites

At one point in your PHP you missed the $ before a variable name, and in an HTML tag you missed quotes, in the submit input i think. Are you using a good editor? ConTEXT with PHP highlighting will pick out variable names for you and change the colour of text that is between quotes, so you can see easily if you closed them right.

Link to comment
Share on other sites

Everything is wrong with the if/else. Don't just follow the instructions online and expect to come out with working code, you need to understand what you're doing before you're able to do it. If you don't understand what an if/else structure does or how it's formed, you're not going to be able to write or fix code that uses them. You need to study and understand the basics before you try to write code.This is your if/else structure, indented. Notice how it's missing a closing bracket for the first opening bracket. Also notice how the error messages don't match up to the correct if statements. The error message that says "Please fill in all fields" is attached to the if statement that checks if the passwords match. And the else block with the error message about the passwords is attached to another else block, not an if block. You also have that random block in the middle with "...." in it, without an else statement or anything before it.

if ($submit){  if ($firstname&&$username&&$password&&$repeatpassword)  {	$password = md5($password);	$repeat = md5($repeatpassword);		if ($password==$repeatpassword);	{	  if (strlen($firstname)>25||strlen($username)>25)	  {		echo "Max character length for first name or username is 25.";	  }	  else	  {		if (strlen($password)>25||strlen(password)<6)		{		  echo "Password is only allowed 6-25 characters long.";		}		{		  ....		}	  }	  else		echo "Your passwords do not match.";	}	else	  echo "Please fill in all fields.";  }

So, this is the point: if you look at that code and don't see anything wrong, then you haven't studied PHP enough to be effective at writing or fixing code.

Link to comment
Share on other sites

it works for him. Why not me
Because he's studied enough to be able to make instructional videos, and you haven't studied enough to be able to write code, that's about as simple as it gets. What separates him from you is study. You may very well be capable of doing this, you just don't understand it yet, you haven't studied enough. An online video isn't enough to understand programming concepts. In order for you to fix this code you need to be able to read the code and understand the structure. If you can understand the structure then you'll be able to look at the code and see why it's not doing what you want it to.
Link to comment
Share on other sites

Now what is wrong with the code?

<?phpecho "<h1>Register</h1>";$submit = $_POST['submit'];//form data$firstname = strip_tags($_POST['firstname']);$username = strip_tags($_POST['username']);$password = strip_tags($_POST['password']);$repeatpassword = strip_tags($_POST['repeatpassword']);$date = date("Y-m-d");if ($submit){//check for existenceif ($firstname&&$username&&$password&&$repeatpassword){//encrypt passwords$password = md5($password);$repeatpassword = md5($repeatpassword);if ($password==$repeatpassword){//check username and first name lengthif (strlen($username)>25||strlen($firstname)>25){echo "Max characters for first name and username is 25.";}else{//check password lengthif {strlen($password)>25||strlen($password)<6){echo "Password must be within 6-25 characters";}else{//register the user}}}elseecho "Your passwords do not match.";}else echo"Please fill in all fields.";}?><html><p><form action='register.php' method='POST'><table><tr><td>Your first name:</td><td><input type='text' name='firstname'></td></tr><tr><td>Choose a username:</td><td><input type='text' name='username'></td></tr><tr><td>Choose a password:</td><td><input type='password' name='password'></td></tr><tr><td>Repeat your password:</td><td><input type='password' name='repeatpassword'></td></tr></table><p><input type='submit' name='submit' value='register'></form></html>

I watched the video very carefully.

Link to comment
Share on other sites

Your if structure is still not formed correctly, you have many of the same problems still. Your code still isn't indented, so it's not going to be easy for you to see the structure unless you indent the code properly. You also used a { where you should have used a ( in one place. PHP should be giving you parse errors about this code, if you aren't seeing error messages you may want to change the PHP settings so that you can see the errors.

Link to comment
Share on other sites

I'm pretty sure at various points in other threads we have recommended to you to read the PHP tutorials here, how did that go? Perhaps it wouldn't hurt to reread them. Start small and work your way up.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...