Jump to content

Form validation.


Cronthenoob

Recommended Posts

If I hit the submit button for my form and there are errors, it displays what the error was and prints out how many errors there are.However, if there are no errors, it prints out that there are no errors, but it doesnt do what its supposed to. (the if $error == 0 part )

if ($_GET['action'] == "newregister") {			$password=$_POST['des_password'];		$verpassword=$_POST['ver_password'];		$ecpassword=md5($_POST['ver_password']);		$des_username=$_POST['des_username'];		$error=0;				$query = "SELECT field FROM table where field='".mysql_real_escape_string($des_username)."'";		$results = mysql_query($query);		echo mysql_error();				if (mysql_affected_rows() > 0) {			echo "<div class='error'>Sorry, the username <strong>".$des_username."</strong> already exists</div>";			$error=$error+1;			$_GET['login'] = "register";		}				if ($password != $verpassword) {			echo "<div class='error'>Your passwords do not match!</div><br /><br />";			$error=$error+1;			$_GET['login'] = "register";		}		echo "you have ".$error." errors!";				if ($error == 0) {			$_GET['login'] == "reg_complete";			$_GET['action'] == "ignoredefaultmain";		}	}

So if the username exists in the database and the passwords match it will echo that there is one error and it will display the error message. If the passwords don't match and the username exists, it will display both error messages and say that there were 2 errors. When there are no errors, it will print that there are 0 errors but it won't do:if($error == 0)

Link to comment
Share on other sites

Your problem appears to be that you're trying to set the values of the $_GET field - since GET is stored in the URL, there's not really a good reason to set it since by the time that happens the URL has already been sent to the browser. You should probably use POST instead anyway, because GET will allow anyone to put their own values in if they know how.In case i'm wrong, insert some echo statements into the $error == 0 code block and see if they're ever reached.

Link to comment
Share on other sites

In case i'm wrong, insert some echo statements into the $error == 0 code block and see if they're ever reached.
They aren't even reached. Nothing happens.and the $_GET im using just loads another section, it doesnt even interact with the database at all.
Link to comment
Share on other sites

		if ($error == 0) {			$_GET['login'] == "reg_complete";			$_GET['action'] == "ignoredefaultmain";		}	}

Ok, i fixed it. for some reason it needed to beif ($error == "0") instead of without " around the 0.I also fixed the == in the $_GET right after i posted it the first time, so that wasnt the issue.If someone knows why i need the quotes around the 0, it'd be some helpful information. Don't think i've had to do that before.Thanks!
Link to comment
Share on other sites

That doesn't make any sense at all, it should be working fine. $error is not a string, it's an integer. Try writing this:var_dump($error);And see what info it tells you about $error. Also, this statement:$error=$error+1;is the same as this:$error++;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...