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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:23:12+00:00 2026-05-29T20:23:12+00:00

I wrote a function in PHP to check username and password from MySQL database

  • 0

I wrote a function in PHP to check username and password from MySQL database and store as session after successful validation. But whatever value I enter to the input box it approves it and made a successful login. My Login code doesn’t work

here it is

function queryByUserAndPass($tableName, $username, $password){
    $queryStatement = "SELECT * FROM ".$tableName." WHERE username='".$username."' 
                      AND password='".$password."' LIMIT 1";
    return $queryStatement;
}

function checkLogIn() {

    if(isset($_POST['submit'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $queryState = queryByUserAndPass("nepal_users", $username, $password);
        if( $resultQuery = mysql_query($queryState) ){
            $found_user= mysql_fetch_array($resultQuery);
            $_SESSION['id']=$found_user['id'];
            $_SESSION['username']=$found_user['username'];
            $message="succesful log in ".$_SESSION['username'];
            header("location:home.php");
            exit;
        }else {
            $message="error in log in";
        }    

    }

}

Please tell me what is wrong in this code and why it is not working.

  • 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-29T20:23:13+00:00Added an answer on May 29, 2026 at 8:23 pm

    you dont check if username and password are correct!

        if( $resultQuery = mysql_query($queryState) ){
            $found_user = mysql_fetch_array($resultQuery);
            if ($username === $found_user['username'] &&
              $password === $found_user['password']) {
                $_SESSION['id']=$found_user['id'];
                $_SESSION['username']=$found_user['username'];
                $message="succesful log in ".$_SESSION['username'];
                header("Location: home.php");
                exit;
            } else {
                echo "wrong username or password";
            }
        }else {
            $message="error from database: " . mysql_errno($resultQuery);
        }
    

    IMPORTANT NOTE

    The code above just illustrates where the logic problem in your code is. There are two other serious issues in it:

    1. Its vulnerable to SQL injection. Using prepared statements is common practise. If prepared statemements are not availabe to you, use something like $SAFE_USER_DATA = array_map('mysql_real_escape_string', $_POST); and use this instead of directly reading $_POSTdata. That way you ensure no malicious user can modify your SQL statements to gain access to your system.

    2. You should never store passwords as plain text – storing a salted SHA1 password hash is encouraged. do NOT simply use md5($password). Attackers can easily decrypt those with the help of rainbow tables

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

Sidebar

Related Questions

I wrote this function in php to check user/pass against account on linux server.
I wrote the following php function to upload files but I'm having a hard
I have a php function I wrote that will take a text file and
I am trying to write a php function to stop MySQL injection attempts. What
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote some ajax code that sends values to a php file for validation
I wrote a simple jQuery function to submit data from a textarea. The script
I have written a function, which has to check whether a username has been
A PHP function I am writing pulls a small bit of HTML data from
Can a function inside the functions.php file call another function from within functions.php? I'm

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.