Whats wrong with my programming logic

Im a newbie. Started learning php about a month ago.

i need help understanding whats wrong with my logic in trying to confirm that the user has submitted the correct info.

Errors

  • email address doesnt seem to verify even when its correctly inputted.

-password also shows error.
Here is the code


<?php

//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs

include ("includes/mass.php");

//Reigstration Form			

$register = "<div id='registration'> 
				Register Here!
				<form action='register.php' method='post'> 
				Username<input type='text' name='username'>
				</br>
				Password<input type='password' name'password'>
				</br>
				Firstname<input type='text' name='firstname'>
				</br>
				Lastname<input type='text' name='lastname'>
				</br>
				email-address<input type='text' name='email'>
				<br>
				<input type='submit' name='submit' value='Sign Up'>
				</form>
			</div>";
			
echo $register;			

//Grabbing data form POST array and storing in variables

$username   = $_POST['username'];
$password   = $_POST['password'];
$firstname  = $_POST['firstname'];
$lastname   = $_POST['lastname'];
$email      = $_POST['email'];
$submit     = $_POST['submit'];



//Check to make sure user has submitted the correct details

	if (isset($submit))
	
		{
				if (strlen($username)<2) // put || ($username==(same as value on the database)
					
					{
		             echo ("<br>You must enter a longer username</br>");
				    }
				
				elseif (strlen($password)<=6) 
					
					{
		             echo ("<br>You must enter a longer password<br>");
				    }
                
				if (strlen($firstname)<=0) 
					
					{
		             echo ("You must enter your firstname<br>");
				    }
				
				elseif (strlen($lastname)<=0) 
					
					{
		             echo ("You must enter your firstname<br>");
				    }

				if ( preg_match('/@/',$email) || (strlen($email)<=6) ) 
					
					{
		             echo ("You must enter a proper email add");
				    }			
		}
	
	else 
	
		{
		//push this information to the database
		echo "successfully submitted your **** to the database";
		}
    
		
?>

It reads: If email contains an @ or is shorter/equal to 6 then it’s invalid.

I don’t know how password can also display an error… you’re not validating the password at all.

Thank you!

it doesnt display an error, what i meant to say was that it still asks for more characters even though i have exceeded the minumum requirement i specified.

preg_match(‘/@/’, $email)
should be
0 === preg_match(‘/@/’, $email)

(at the very least)

Ah, I didn’t notice the password validation code earlier; admit it, did you edit the post? :wink: Or am I going blind?


Password<input type='password' name'password'>

Missing equals sign.

awwrrrr darn it. ya got me :).

wow. i missed that. thanks for spotting it mate.

Off Topic:

preg_match(‘/@/’, $email)
should be
0 === preg_match(‘/@/’, $email)

(at the very least)


1 !== preg_match($pattern, $subject)

This would be better as preg_match can return 0 or false on an unsuccessful match.

Nope, preg_match only returns false if it gets an error (and in that case, the error should be shown on the page and fixed).

The function returns how many times it matched, so always 0 or 1.

I disagree, you’re checking for a match, no match is found so the block should be executed.

I don’t really want to be going off on a tangent about error handling and bring the thread off-topic, however you are right. An error in the pattern should be looked at, whether this is the place for that is another thread.