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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:01:00+00:00 2026-06-11T03:01:00+00:00

I have the following code, and it will not work. I am currently working

  • 0

I have the following code, and it will not work. I am currently working on a simple change password feature for a system and cant get it to function correctly. i was wondering if i was overlooking a really simple solution?

<?php    
    $con = mysql_connect("localhost","root");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    $username = $_POST['userid'];  
    $password = $_POST['cpword'];
    $newpassword = $_POST['pword'];
    $confirmnewpassword = $_POST['pword2'];

    $result = mysql_query("SELECT username, pword FROM login WHERE username='$username'");

    if(!$result) {
        echo "The username entered does not exist!";
    } else
        if($password != mysql_result($result, 0)) {
            echo "Entered an incorrect password";
        }

    if($newpassword == $confirmnewpassword) {
        $sql = mysql_query("UPDATE login SET pword = '$newpassword' WHERE username = '$username'");     
    }

    if(!$sql) {
        echo "Congratulations, password successfully changed!";
    } else {
        echo "New password and confirm password must be the same!";
    }       
?>
  • 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-11T03:01:01+00:00Added an answer on June 11, 2026 at 3:01 am

    OK there are alot of things wrong with this code so I’ve rewritten it to be up to date and not at all dangerous (mostly).

    I’m not hashing the passwords here like you really should but you can do it quite simply with a bit of reading ( try this: Secure hash and salt for PHP passwords )

    For a comprehensive list of the exact problems and solutions to those problems, take a look at @Bondye’s post or the comments on the OP’s question.

    Disclaimer: not tested so might have a few syntax errors. this is still not fantastic but it’s a much better starting point than the original code. See below for a list of what I’ve changed and why.

    Here goes…

    <?php
        $host = "localhost";
        $database = "yourdatabase";
        $username_db = "root";
        $password_db = "databasepassword";
        $con = mysqli_connect($hostname, $username_db, $password_db, $database) or die(mysqli_error($con));
    
        $username = $_POST['userid'];  
        $newpassword = $_POST['pword'];
        $confirmnewpassword = $_POST['pword2'];
    
        if($newpassword == $confirmnewpassword)
        {
            //password & password confirm match, do the update
            $query = sprintf("UPDATE login SET pword=%s WHERE username=%s",
                              mysql_real_escape_string($newpassword),
                              mysql_real_escape_string($username));                    
            $sql = mysqli_query($query, $con) or die(mysqli_error($con);     
            if($sql)
            {
                echo "Congratulations, password successfully changed!";
            } 
            else
            {
                //sql error or update didn't work?
                echo 'generic failure message';
            }        
        }
        else
        {
            //new password and confirm password weren't the same.
            echo "New password and confirm password must be the same!";
        }    
    
    ?>
    

    Changes:
    Removed the query to look for a username – personal choice really but I don’t see the point in querying the DB to see if the user exists when you are going to be doing an implicit search for that user in the update query. Secondly, you should have logged them in before they can access this script, so there should be no question about whether the user exists or not.

    Changed mysql functions to their mysqli equivelants.

    Simplified and cleaned up the mess of if tests and put validation before the query itself. This is better as you shouldn’t tell your users what you have in your database as this is useful information for attackers and not at all useful for users who should already be logged in by this point.

    Hope this helps and open to corrections.

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

Sidebar

Related Questions

I have following code in my Application. Comments in my code will specify My
I have the following code which will generate two pdf files containing plots to
I have the following code which will allow only numbers 0-9. But i want
I have the following code that will rotate a drawable by a set amount
I have this following code which will return all the current semesters. How do
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have the following code, which will retrieve a filename from a table and
I have the following code, which will output the child-elements of the weather element.
I have the following XML code. You will notice the tag Description is repeated,
I’m after some C# code that will have the following methods that will return

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.