signing in code not working
Posted: Tue Mar 06, 2012 12:34 pm
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:
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';
?>