IF statement problems php

ive been following a tutorial for login and registration but im stuck on the validation.
The error for required fields works, so do the password length and matching password.
for some reason the user_exists function wont work on registration but works fine with login.
[php]<?php
include ‘init.php’;
include ‘header1.php’;
include ‘navbar.php’;

if(empty($_POST) === false) {

$required_fields = array('username', 'password', 'password_again', 'first_name', 'last_name', 'email');
foreach($_POST as $key=>$value) {
	if (empty($value) && in_array($key, $required_fields) === true) {
		$errors[] = 'Fields marked with an asterisk are required!';
		break 1;
	}
}

if (empty($errors) === true) {
	if (user_exists($_POST['username']) === false) {
				$errors[] = 'Sorry, the username\'' . $_POST['username'] . '\' is already taken.';
}
if(strlen($_POST['password']) < 6) {
	$errors[] = 'Your password must be at least 6 characters';
}
if ($_POST['password'] !== $_POST['password_again']) {
	$errors[] = 'Your passwords do not match';
}
}

}

print_r($errors);

?>[/php]

now if i change

[php]if (user_exists($_POST[“username”]) === true) {[/php]

to

[php] if (user_exists($_POST[“username”]) === false) { [/php]

it returns the error that the username already exists which shows the IF statement is working so could only imagine the problem is in my user_exists function, but this function works fine with the login, anyway im total baffled by this so heres my user_exists function.

[php]function user_exists($username) {
$username = sanitize($username);
$query = mysql_query(“SELECT COUNTuser_id FROM Users WHERE username = ‘$username’”);
return (mysql_result($query, 0) == 1) ? true : false;
}[/php]

any help is massively appreciated!

Matthew

Sponsor our Newsletter | Privacy Policy | Terms of Service