Page 1 of 1

signing in code not working

Posted: Tue Mar 06, 2012 12:34 pm
by beginner123
I'm making a page for signing into a forum but its not working right. If I just enter the username it submits instead of outputing the error message the username field must not be empty.
Heres all the code:

Code: Select all

<?php
//signup.php
include 'connect.php';
include 'header.php';

echo '<h3>Sign up</h3><br />';

if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    echo '<form method="post" action="">
 	 	Username: <input type="text" name="userName" /><br />
 		Password: <input type="password" name="userPassword"><br />
		Password again: <input type="password" name="userPassword"><br />
		E-mail: <input type="email" name="userEmailAddress"><br />
 		<input type="submit" value="Sign up" />
 	 </form>';
}
else
{
	$errors = array(); 
	
	if(isset($_POST['userName']))
	{
		if(!ctype_alnum($_POST['userName']))
		{
			$errors[] = 'The username can only contain letters and digits.';
		}
		if(strlen($_POST['userName']) > 30)
		{
			$errors[] = 'The username cannot be longer than 30 characters.';
		}
	}
	else
	{
		$errors[] = 'The username field must not be empty.';
		
	}
	
	
	if(isset($_POST['userPassword']))
	{
		if($_POST['userPassword'] != $_POST['userPassword'])
		{
			$errors[] = 'The two passwords did not match.';
			echo '<a href="signup.php">Go back to sign up page</a>';
		}
	}
	else
	{
		$errors[] = 'The password field cannot be empty.';
		echo '<a href="signup.php">Go back to sign up page</a>';
	}
	
	if(!empty($errors)) 
	{
		echo 'You must fill in all fields to sign up.<br /><br />';
		echo '<a href="signup.php">Go back to sign up page</a>';
		echo '<ul>';
		foreach($errors as $key => $value) 
		{
			echo '<li>' . $value . '</li>'; 
		}
		echo '</ul>';
	}
	else
	{		
		$sql = "INSERT INTO
					users(userName, userPassword, userEmailAddress ,userDate, userLevel)
				VALUES('" . mysql_real_escape_string($_POST['userName']) . "',
					   '" . sha1($_POST['userPassword']) . "',
					   '" . mysql_real_escape_string($_POST['userEmailAddress']) . "',
						NOW(),
						0)";
						
		$result = mysql_query($sql);
		if(!$result)
		{
			echo 'Something went wrong while registering. Please try again later.';
			echo '<a href="signup.php">Go back to sign up page</a>';
			echo mysql_error(); 
		}
		else
		{
			echo 'Succesfully registered. You can now <a href="signin.php">sign in</a> and start posting';
		}
	}
}

include 'footer.php';
?>


Re: signing in code not working

Posted: Tue Mar 06, 2012 4:02 pm
by social_experiment
Try the changes suggested below

Code: Select all

<?php
if(isset($_POST['userName'])) // change to (!empty($_POST['userName']))
#
if(isset($_POST['userPassword'])) // change to if (!empty($_POST['userPassword']))
?>

Re: signing in code not working

Posted: Tue Mar 06, 2012 4:03 pm
by requinix
isset() only checks that the thing exists, not that it has a value. You can use !empty() for that.

Separately, for the password: you can't have two fields with the same name. Give them different names.

Re: signing in code not working

Posted: Wed Mar 07, 2012 6:47 am
by beginner123
ok so I changed isset to !empty but it still doesn't work. I'm am still able to only enter a user name and leave the rest blank and it creates an account

Re: signing in code not working

Posted: Wed Mar 07, 2012 9:33 am
by social_experiment
Can you paste the modified code? i only changed the isset to !empty and the script worked (i.e wouldn't let me 'submit' without the two fields completed)

Re: signing in code not working

Posted: Wed Mar 07, 2012 10:36 am
by beginner123
I found the error it works now. Thanks for the help :)
maybe you could help with something else?
viewtopic.php?f=1&t=134742