Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:04:45+00:00 2026-06-15T04:04:45+00:00

I want PHP to make sure the username is not already used and also

  • 0

I want PHP to make sure the username is not already used and also check to see if the field is empty. Sorry I am a huge noob when it comes to php. Here is my code:

// Check for an Username:
    $dup = mysql_query("SELECT user_username FROM users WHERE user_username='".$_POST['user_username']."'");
        if(mysql_num_rows($dup) >0){
            $errors[] = 'Username already used.';
        }
        else{
            $un = mysqli_real_escape_string($dbc, trim($_POST['user_username']));
            echo '<b>Congrats, You are now Registered.</b>';
        }
        else {
            $errors[] = 'You forgot to enter your Username.';
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:04:46+00:00Added an answer on June 15, 2026 at 4:04 am

    You have two else statements, which cannot be used. Also, you need to escape your $_POST data. Lastly, you should move away from mysql_* functions as that time has passed. Use either mysqli or PDO.

    <?php
    $sql = sprintf("SELECT user_username FROM users WHERE user_username='%s'",
            mysql_real_escape_string($_POST['user_username']));
    $dup = mysql_query($sql);
    if(empty($_POST['user_username'])){
        $errors[] = 'You forgot to enter your Username.';
    }elseif(mysql_num_rows($dup) >0){
        $errors[] = 'Username already used.';
    }else{
        $un = mysqli_real_escape_string($dbc, trim($_POST['user_username']));
        echo '<b>Congrats, You are now Registered.</b>';
    }?>
    

    An alternative, and easier flow in my opinion, would be a try/catch statement. This way you don’t have to make an unnecessary database call if the username is empty:

    <?php
    try{
        if(empty($_POST['user_username'])){
            throw new Exception('You forgot to enter your Username.');
        }
    
        $sql = sprintf("SELECT user_username FROM users WHERE user_username='%s'",
            mysql_real_escape_string($_POST['user_username']));
        $dup = mysql_query($sql);
    
        if(mysql_num_rows($dup) >0){
            throw new Exception('Username already used.');
        }
    
        echo '<b>Congrats, You are now Registered.</b>';
    
    }catch(Exception $e){
        echo $e->getMessage();
    }?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make sure my php code and my website does not use
To allow caching a PHP generated file, I want to make sure, that the
I want to make sure people can't type the name of a PHP script
I want to make sure AJAX responses from dynamic JSON pages does not slow
I want to make sure a file path set via query string does not
I'm creating a new PHP application and want to make sure I get the
i've written a daemon in php and want to make sure it doesn't leak
i want to make a php app that let people submit photos/videos/sounds Now, everything
I want to make my php page only accessible from another page redirect and
i have two strings in a php i want to make following checks in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.