sparkones
sparkones

Reputation: 39

Display error message PHP Mysql

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!

Upvotes: 0

Views: 25204

Answers (6)

Stefan Fandler
Stefan Fandler

Reputation: 1141

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

Upvotes: 0

Arun Killu
Arun Killu

Reputation: 14233

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.

Upvotes: 2

RaJeSh
RaJeSh

Reputation: 313

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>";  
           }  
   } } ?>

Upvotes: 0

Chetana Kestikar
Chetana Kestikar

Reputation: 570

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.

Upvotes: 0

sicKo
sicKo

Reputation: 1256

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());

Upvotes: 0

Akhilraj N S
Akhilraj N S

Reputation: 9467

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);
        }
        }

Upvotes: 1

Related Questions