0

I want to allow users to either have there username field empty at any time but I get the username error message Your username is unavailable! how can I correct this problem?

Here is the PHP code.

if(isset($_POST['username'])) {

    $u = "SELECT * 
          FROM users 
          WHERE username  = '$username'
          AND user_id <> '$user_id'";
    $r = mysqli_query ($mysqli, $u) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));

    if (mysqli_num_rows($r) == TRUE) { // Unavailable.
        echo '<p class="error">Your username is unavailable!</p>';
        $username = NULL;
    } else if(mysqli_num_rows($r) == 0) { // Available.
        $username = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['username']))));
    }
}

NONE of these examples are working. I want to know how can I let my users have no username?

2 Answers 2

1
if(isset($_POST['username']) && trim($_POST['username'])!=='') {

Otherwise, $_POST['username'] will be set even if the form is submitted with an empty field.

Sign up to request clarification or add additional context in comments.

Comments

0
if(isset($_POST['username']) && !empty($_POST['username'])) {
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.