Php Logic Problem

Hello, I’ve been stuck on my logic forever, I don’t quite see what’s wrong It’s a register script, I’m trying to check if username is taken, passwords match, and none of the fields are empty. Here’s the code:

[code]<?php

require(“includes/Connect.php”);
mysql_select_db(“divnx5_web”);

$username = $_POST[‘user’];
$password = sha1($_POST[‘pass’]);
$password2 = sha1($_POST[‘pass2’]);
$email = $_POST[‘email’];

function error($result, $query)
{
if(!$result)
{
echo “Query Failed: $query
” . mysql_errno() . ‘
’ . mysql_error();
}
}

function check($username)
{
$query = “SELECT username FROM users WHERE username = ‘$username’”;
$result = mysql_query($query) or die(“Problem with the query: $query
” . mysql_error());

  if (mysql_num_rows($result) > 0)
     echo "The username " . $username . ' is already taken!<br /><br />';

}

if(!$con)
{
echo “Registering is disabled right now, please check back later”;
}
else
{
if(!$_POST[‘register’])
{
echo "

Use the following form to register a new account with us.



Username:



Password:



Confirm Password:



E-mail Address:






";
}
else
{
if(!isset($username) || empty($username))
{
echo “The username you entered encountered a problem
”;
check($username);
}

 if(!$password || !$password2 || !isset($password) || !isset($password2) || empty($password) || empty($password2))
 {
   echo "The password field(s) cannot be left empty.<br />";
 }

 if(!$email || !isset($email) || empty($email))
 {
   echo "The email you entered encountered a problem<br />";
 }

 if($password != $password2)
 {
    echo "The passwords you entered do not match<br />";
 }
 else
 {
    if(isset($username) && $password == $password2 && isset($email))
    {
       $query = "INSERT INTO users(username, password, email, rank) VALUES('$username', '$password', '$email', '1')";
       $result = mysql_query($query);
       error($result, $query);
       mysql_close($con);
       echo "Thank You for registering with us " . $username . '! Before you can login and start using the features of the site, please check the email you provided (' . $email . ') for a confirmation link.';
    }
 }

}
}

?>[/code]

The problem is, if the username is taken, or the email field is left empty, it still adds you to the database. If you leave all fields empty, it will print out some errors that I put in echo but it still adds a blank space to the db.

I checked it over and over as I mentioned before. Help would be greatly appreciated.

Thank You. :)

Well First of all, There has to be a better way to get through the logic than all the nested IF statements.

However, as I weeded through some of this I see that your “Check” for the username is solely based on that it’s either EMPTY or that the variable $username is NOT set. So the only way it will “CHECK” username is if you Submit the form (Register) and you don’t pass a username in the field provided.

Even better you “CHECK” it AFTER you already have determined it’s a problem

// Only gets into this IF when form is submitted without a username.
if(!isset($username) || empty($username))
{
     echo "The username you entered encountered a problem<br />";
    check($username); // Check is done AFTER you determined (above) that there is a problem.
}

Additionally the CHECK function does NOTHING to stop the registration process, rather it only echoes that the name is already taken (which it NEVER will because it only does the check (based on the code above) if NO NAME has been submitted.)

Thanks for mentioning that.

Sponsor our Newsletter | Privacy Policy | Terms of Service