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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:51:32+00:00 2026-05-31T08:51:32+00:00

I am experimenting with PHP and MySQL, but now I found a PHP code

  • 0

I am experimenting with PHP and MySQL, but now I found a PHP code that I want to use, but it’s vulnerable for SQL injection. Does anyone know what the injection is and what code can fix it?

    <?
        // both the username and password must be specified
        if(!isset($_GET["username"]) || !isset($_GET["password"])) {
                print "NO username and/or password";
                die;
        }
        $username = $_GET["username"];
        $password = $_GET["password"];

        // prepare database connection
        mysql_connect("host", "user", "password");
        mysql_select_db("dbase");

        // validate the specified username
        $res = mysql_query("SELECT id FROM users WHERE username = '$username'");

        if(!$res || !($row = mysql_fetch_row($res))) {
                print "Unknown username";
                die;
        }
        // validate the password
        $res = mysql_query("SELECT password FROM users WHERE id = ".$row[0]);
        if(!$res || !($row = mysql_fetch_row($res))) {
                print "Illegal userid record";
                die;
        }
        if($password != $row[0]) {
                print "Invalid password";
                die;
        }
        // Access Granted
?>

The code is part of an extra code but that one isn’t necessary, the username can be selected with /somecode.php?username=’ OR ‘1’=’1&password=something but ‘ OR doesn’t work with the password, could anyone help me?

Regards

  • 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-31T08:51:33+00:00Added an answer on May 31, 2026 at 8:51 am

    Do not build your SQL code dynamically – pass user input as parameters.

    See this example in the PHP documentation.

    If you dynamically build your SQL like this:

    $res = mysql_query("SELECT id FROM users WHERE username = '$username'");
    

    where $username could contain something which was directly provided by a user, then you are at risk. Using a solution such as a prepared statement, where it is not possible for a user to directly influence the actual SQL query being executed, is a safer alternative.

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

Sidebar

Related Questions

I'm experimenting by making a social network from scratch in PHP/MySQL, but I'm having
I'm experimenting with PHP's session_set_save_handler and I'd like to use a PDO connection to
I installed everything separately (Apache, PHP, MySQL and phpMyAdmin) and do not use a
Using this php code: try{ $dbh = new PDO(mysql:host=$host;dbname=$dbname,$user,$pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //
I am experimenting with a test database to learn SQL, (MySQLi PHP) I am
So I'm doing some experimenting with PHP/Apache. Let's say I have this code. <div>DIV
I am experimenting with php unlink() , what I want to do is run
I have a server (VPS) that hosts numerous PHP / MySQL websites. Most are
I'm experimenting on this code that I got from the net (I'm trying to
I'm currently experimenting with a WordPress website that uses a modified search.php file to

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.