0

i am unable to get the last 2 echos to work, even if the update query fails it still displays success. If anyone has any suggestions on this code to be improved on any line, please do!

<?php
        if(!empty($_POST['username']) && !empty($_POST['answer']))  { 
            $username = $_POST['username'];
            $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it
            or die(mysql_error());
            $fetched = mysql_fetch_array($idfetch);  
            $id = $fetched['id']; //get users id for checking
            $answer = $_POST['answer'];
            $password = (mysql_real_escape_string($_POST['password']));
            $confpass = (mysql_real_escape_string($_POST['confpass']));
            if ($password != $confpass) {
                echo ("Passwords do not match, please try again.");
                exit;
            }
            $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'");
            if($updatequery)  {  
                echo "<h1>Success</h1>";  
                echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>";  
            }  
            else  {  
                echo "<h1>Error</h1>";  
                echo "<p>Sorry, but a field was incorrect.</p>";  
            }  
       } 
?>

Thanks in advance!

4
  • Please don't use mysql_query in new applications. It's terribly dangerous if not used perfectly which is an enormous nuisance to do, though I've seen you're at least trying here. You escaped two out of three variables and introduced a gigantic injection hole, though. Close enough is not good enough on the public internet. At the very least you should be using PDO unless you have a very good reason because when using SQL placeholders these mistakes are usually non-existent. Commented Dec 17, 2012 at 6:54
  • Thanks for this comment, i appreciate it and i will look into this. I haven't seen anything on it but ill do my research! Commented Dec 17, 2012 at 7:31
  • Thanks everyone for all the comments i appreciate it. Commented Dec 17, 2012 at 7:32
  • There's several tutorials on how to use PDO effectively. If you haven't seen anything about it, you need better reference material. mysql_query is a relic of the 1990s. Commented Dec 17, 2012 at 15:37

6 Answers 6

2
mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'") or die(mysql_error()."update failed");

and use

mysql_affected_rows()

Returns the number of affected rows on success, and -1 if the last query failed.
Sign up to request clarification or add additional context in comments.

Comments

1

use try catch and try to get the error enable error reporting in php also

<?php
        error_reporting(E_ALL);
        ini_set('display_errors','On');
        if(!empty($_POST['username']) && !empty($_POST['answer']))  { 
        $username = $_POST['username'];
        $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it
        or die(mysql_error());
        $fetched = mysql_fetch_array($idfetch);  
        $id = $fetched['id']; //get users id for checking
        $answer = $_POST['answer'];
        $password = (mysql_real_escape_string($_POST['password']));
        $confpass = (mysql_real_escape_string($_POST['confpass']));
        if ($password != $confpass) {
        echo ("Passwords do not match, please try again.");
        exit;}

        try{
        $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'");
        if($updatequery)  {  
        echo "<h1>Success</h1>";  
        echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>";  }  
        else  {  
        echo "<h1>Error</h1>";  
        echo "<p>Sorry, but a field was incorrect.</p>";  
        }  

        }catch(Exception $e){
            print_R($e);
        }
        }

Comments

0

use or die(mysql_error()) as it will display mysql error if there is an error with your query.

$updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'") or die(mysql_error());

Comments

0

Try this:

$idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'");
if(!idfetch){
  die(mysql_error());
}

Do the same for all other queries too.

Comments

0

try this, first count the row count value its great 1 then proceed the login process.

<?php
    if(!empty($_POST['username']) && !empty($_POST['answer']))  { 
        $username = $_POST['username'];
        $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it
        or die(mysql_error());
        $fetched = mysql_fetch_array($idfetch);

        $count= mysql_num_rows($idfetch);

        if($count>0){
        $id = $fetched['id']; //get users id for checking
        $answer = $_POST['answer'];
        $password = (mysql_real_escape_string($_POST['password']));
        $confpass = (mysql_real_escape_string($_POST['confpass']));
        if ($password != $confpass) {
            echo ("Passwords do not match, please try again.");
            exit;
        }

        $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'");

          if($updatequery)  {  
            echo "<h1>Success</h1>";  
            echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>";  
         }  
           else  {  
             echo "<h1>Error</h1>";  
             echo "<p>Sorry, but a field was incorrect.</p>";  
           }  
   } } ?>

Comments

0

Use

if(mysql_num_rows($updatequery)  > 0) {
    // success
} else {
    // error
}

$updatequery will always be true (not NULL), until there is an error in your query

2 Comments

Wouldn't mysql_affected_rows() be the right function instead?
@DamienPirsy is correct. From the manual: mysql_affected_rows() - Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query. mysql_num_rows() - Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.