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 5970141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:19:54+00:00 2026-05-22T20:19:54+00:00

I have a form on a website that needs validating before entering the form

  • 0

I have a form on a website that needs validating before entering the form data into the database.

Its checking whether a username already exists by user the mysql_num_rows function. But i cannot seem to get it working. When testing, it doesn’t let a new username be added.

Here is the full code being used:

<?php
session_start();

include("databaseConnect.php");
// Insert a row of information into the table "example"


// check if username is already in database
if(mysql_num_rows(mysql_query("SELECT userName FROM registeredUsers WHERE userName =     '$_POST[userName]'"))){
 echo "Username: ". $_POST[userName]." already exists in the Database<br><br>";
    echo "You will be redirect back to the form in 5 seconds";
$ref = $_SERVER['HTTP_REFERER'];
header( 'refresh: 5; url='.$ref);

//check if hemis is already in database
}elseif(mysql_num_rows(mysql_query("SELECT hemis FROM registeredUsers WHERE hemis = '$_POST[hemis]'"))){
echo "Student [Hemis] Number: ". $_POST[hemis]." already exists in the Database<br><br>";
echo "You will be redirect back to the form in 5 seconds";
$ref = $_SERVER['HTTP_REFERER'];
header( 'refresh: 5; url='.$ref);


// if all the conditions above are fine, it will insert the data to MySQL
}else{
  mysql_query("INSERT INTO registeredUsers
(firstName, lastName, hemis, userName, MAC) VALUES('$_POST[firstName]', '$_POST[lastName]', '$_POST[hemis]', '$_POST[userName]', '$_POST[mac]' ) ")
or die(mysql_error());

echo "Data Inserted! <br><br>";
}

Thanks a lot 🙂

  • 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-05-22T20:19:55+00:00Added an answer on May 22, 2026 at 8:19 pm

    I’d rewrite this completely. It’s subject to SQL injection, inefficient and a little too terse. Plus, you’re generally better off using the PHP mysqli extension

    Also, make sure you enclose the $_POST variable names in quotes. You’ve written them as constants, as opposed to strings. (Unless you’ve defined constants elsewhere in code that represent the string values, this is an error. Turn on PHP warnings while you’re developing.)

    $safe_username = mysqli_real_escape_string($_POST['userName']);
    $sql = "SELECT userName FROM registeredUsers WHERE userName='$safe_username' LIMIT 1";
    $result = mysqli_query($database_connection, $sql);
    if (mysqli_num_rows($result))
    {
        // username already found code
        mysqli_free_result($result);
    }
    else
    {
        $safe_hemis = mysqli_real_escape_string($_POST['hemis']);
        $sql = "SELECT hemis FROM registeredUsers WHERE hemis='$safe_hemis' LIMIT 1";
        // Side note, LIMIT 1 tells the database engine to stop looking after it's found one hit. More efficient as you're only looking for a Boolean value anyway.
        $result = mysqli_query($database_connection, $sql);
        if (mysqli_num_rows($result))
        {
            // hemis found code
            mysqli_free_result($result);
        }
    }
    

    The rest you can probably figure out from this.

    Please validate and escape all input. Validation includes checking for sanity – is the data within bounds (string length, numerical bounds, etc), etc. All input is evil!

    You really don’t want to depend on HTTP_REFERER either. User agents don’t always pass referrers.

    Also, I know it’s not a big deal, but use CSS rather than <br>. If you’re using the XHTML doctype, you have to properly close all tags, so <br> would become <br />. This is a good idea anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django form that needs to do some client-side validation before submitting
I have a registration form for a website that needs to check if an
I have a website form that requires a US phone number input for follow
On a website if I have a form where the user can input some
We have a simple registration form for our website users where we only require
I have a website with a contact form. User submits name, email and message
We have several wizard style form applications on our website where we capture information
I have a website where we use Javascript to submit the login form. On
I have a website laid out in tables. (a long mortgage form) in each
I have form that displays several keywords (standard set of choice lists that changes

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.