tags:

views:

49

answers:

1

$sql = "UPDATE....";

if(mysql_query($sql))
    {
        $_SESSION['Trans']="COMMIT";
        header("location:result.php");
        exit;
    }
    else 
        {
            $_SESSION['Trans']="FAIL";
            $_SESSION['errors'] = "Error: Sorry! We are unable to update your Profile, Please contact to PNP HelpDesk.";
            header("location:result.php");
            exit;
        }//end  IF

data is getting updated then why compiler is not coming inside IF condition.

+2  A: 

mysql_query only returns FALSE on an error condition, rather than if no rows have been updated.

To see if anything has been updated, use mysql_affected_rows, e.g.:

$sql = 'UPDATE ....';
mysql_query($sql);
if (mysql_affected_rows() > 0) { 
  // Success
} else {
  // Failure
}
Chris Smith
Although it doesn't,For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. But I would still assign it to a variable and test that, perhaps with a more implicit `if($result === true)`
DavidYell
@DavidYell So it does. I had a cursory check of the docs and the return type is given as `resource` rather than `mixed`, so didn't read on. Either way though, an UPDATE that affects no rows is still "successful", so you still need `mysql_affected_rows` :)
Chris Smith
it returns FALSE on an error. No row updated is no error but from the database POV valid correct behavior, depending on the data and where clause.
johannes
@Chris nope still it is not working.
Lisa Ray
@Lisa Are you sure your query isn't erroring? Try `mysql_query($sql) or die(mysql_error());`
Chris Smith