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 am developing a website which needs to contain functionality in which the user
I have a windows form application which has a text box and 3 buttons.
I'm trying to make a script that scrapes a website to retrieve the latest
I have created a Check Out wizard utilizing the jQuery UI Tabs in an
I am attempting to implement xmpp chat/collaboration between two C# desktop clients that exchange
My question is. I've a barcode-scanner on my website in order to verify if
I am using the WebBrowser control on a Windows Forms application in Visual Studio
I am using ColorBox as a lightbox library. I want to use it to
First time asking a question here. Usually I can find my answer without having

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.