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.