signing in code not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

signing in code not working

Post 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';
?>

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: signing in code not working

Post 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']))
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: signing in code not working

Post 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.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: signing in code not working

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: signing in code not working

Post 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)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: signing in code not working

Post 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
Post Reply