I am trying to check my database for a key that has already been put in. If the key exists then I need it to check to make sure the `username` field hasn't been filled. If it has then it needs to throw an error so that it doesn't update and overwrite the information already stored in the database.

Everything works. The update functions etc. the only part that does not work is the checking if the key exists and if the `username` portion is filled(not sure exactly how to do that) before updating the database. 

Thanks,

Cameron Andrews

Code:

```
// If the Register form has been submitted

$err = array();

if(strlen($_POST['username'])<4 || strlen($_POST['username'])>32){
    $err[]='Your username must be between 3 and 32 characters!';
}

if(preg_match('/[^a-z0-9 _]+/i',$_POST['username'])){
    $err[]='Your username contains invalid characters!';
}

if(!checkEmail($_POST['email'])){
    $err[]='Your email is not valid!';
}

 $resultN = mysql_query("SELECT * FROM Users WHERE key='".$_POST['kgen']."'");
 while($row = mysql_fetch_array($resultN))//for the results that are returned set the local variables
      {
        if($_POST['kgen'] == $row['key']){
            if($_POST['username'] == $row['usr']){
                $err[]='Username already in use';
            }
        }else if($_POST['kgen'] == ""){
            $err[]='Invalid Key Code!';

        }else{
            $err[]='Error occured please try again';
        }
     }

if(!count($err)){
    // If there are no errors
    $_POST['email'] = mysql_real_escape_string($_POST['email']);
    $_POST['username'] = mysql_real_escape_string($_POST['username']);
    $_POST['pass'] = mysql_real_escape_string($_POST['pass']);
    // Escape the input data
    $theName = $_POST['name'];
    $theUser = $_POST['username'];
    $thePass = $_POST['pass'];
    $theEmail = $_POST['email'];
    $theType = "member";
    $theRegIP = $_SERVER['REMOTE_ADDR'];
    $theDate = "NOW()";
    $theKey = $_POST['kgen'];

        // If everything is OK register
        mysql_query("UPDATE cad.Users SET name = '$theName', usr = '$theUser', pass = '$thePass', email = '$theEmail', type = '$theType', regIP = '$theRegIP', dt = '$theDate' WHERE Users.key = '$theKey'");

```