How to check whether query executed or not

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vkas
    New Member
    • Feb 2009
    • 78

    How to check whether query executed or not

    i had created a form ! having
    three text box

    Old password
    New password
    Confim new password!!


    it is being validated by javascript with the following fuction

    Code:
    <script language="javascript">
    
    function checkempty(obj,msg){
    	if(obj.value==''){
    		alert(msg);
    		obj.focus()
    		return false;
    	}
    	return true;
    }
    function confirmpass(obj,obj1,msg){
    	if(obj1.value!=obj.value){
    		alert(msg);
    		obj1.select()
    		obj1.focus()
    		return false;
    	}
    	return true;
    }
    function chkChangePass(){
    	if(checkempty(document.frmChangePass.oPass,"Information: Enter Old Password")==false) return false;
    	if(checkempty(document.frmChangePass.NPass,"Information: Enter New Password")==false) return false;
    	if(checkempty(document.frmChangePass.CNPass,"Information: Confirm New Password")==false) return false;
    	if(confirmpass(document.frmChangePass.NPass,document.frmChangePass.CNPass,"Information: New and Confirm Password should be same")==false) return false;
    	return true;
    }
    </script>
    The form code is as follow
    Code:
    <form name="frmChangePass" method="post" action="Changepass.php?action=Confirm&amp;id=1" onSubmit="return chkChangePass();">
               
              <table width="100%" border="0" cellspacing="2" cellpadding="1">
                  <tr bordercolor="#CCCCCC">
                    <td width="30%">Old Password:</td>
                    <td width="70%" align="left" valign="middle"><input name="oPass" type="password" onfocus="this.select();" value="<?php if(isset($_REQUEST["oPass"])){print $_REQUEST["oPass"];}?>" style="BORDER-WIDTH:1;BORDER-STYLE:dashed;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" id="oPass" size="54" /></td>
                  </tr>
                  <tr bordercolor="#CCCCCC">
                    <td>New Password: </td>
                    <td align="left" valign="middle"><input name="NPass" type="password" onfocus="this.select();" value="<?php if(isset($_REQUEST["NPass"])){print $_REQUEST["NPass"];}?>" style="BORDER-WIDTH:1;BORDER-STYLE:dashed;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" id="NPass" size="54" /></td>
                  </tr>
                  <tr bordercolor="#CCCCCC">
                    <td>Confirm New Password: </td>
                    <td align="left" valign="middle"><input name="CNPass" type="password" onfocus="this.select();" value="<?php if(isset($_REQUEST["CNPass"])){print $_REQUEST["CNPass"];}?>" style="BORDER-WIDTH:1;BORDER-STYLE:dashed;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" id="CNPass" size="54" /></td>
                  </tr>
                  
                  <tr bordercolor="#FFFFFF" bgcolor="<?php echo $rClr?>">
                    <td align="left" valign="middle" bordercolor="#CCCCCC" bgcolor="<?php echo $rClr?>" class="welcome">&nbsp;</td>
                    <td align="left" valign="middle" bordercolor="#CCCCCC" bgcolor="<?php echo $rClr?>" class="Vkasimp"><input type="submit" name="Submit2" value="Change Pass" style="BORDER-WIDTH:1;BORDER-STYLE:double;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" /></td>
                  </tr>
                </table>
                </form>

    according to action of the form on submission the below code is executed
    it get the id,old password and new password
    and execute the update query

    Code:
    	
    <?php }if($action=="Confirm"){
    $id=$_GET['id'];
    $oldpassword=$_POST['oPass'];
    $CNpassword=$_POST['CNPass'];
    
    $update= "Update `users` Set `user_pass`='$CNpassword' Where `campus_id`=1 AND `user_pass`='$oldpassword'";
    $result=mysql_query($update) or die(mysql_error()); 
    
    php echo "<strong>Password successfully changed</strong>"; 
    }

    if the old password is wrong or unmatched the query does not executes and password is not changed in the data base


    i want to make check that if the query is not executed or the password is not changed due to unmatched old password than it should display


    old password incorrect please correct it again

    else it shows password changed successfully


    my code simply show password changed successfully!! on both states!

    i had used if else but not working
    please help me
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Show us what you tried.

    When you want to check how many rows were successfully updated with an UPDATE query, you should use the mysql_affected_ rows function. People often mistakenly use the mysql_num_rows function, but that function only counts the number of rows returned, and an UPDATE query never returns any rows.

    Comment

    • Vkas
      New Member
      • Feb 2009
      • 78

      #3
      i have only 1 row in the table simply

      can u tell what would be the return type of the mysql affected rows function ?

      can it be done like this


      Code:
      <?php }if($action=="Confirm"){
      $id=$_GET['id'];
      $oldpassword=$_POST['oPass'];
      $CNpassword=$_POST['CNPass'];
      
      $update= "Update `users` Set `user_pass`='$CNpassword' Where `campus_id`=1 AND `user_pass`='$oldpassword'";
      $result=mysql_query($update) or die(mysql_error()); 
      [B]if(mysql_affected_rows ($result) == false)[/B]
      {
       echo "<strong> Old password wrong please retype it again<</strong>"; 
      }
      else
      {
       echo "<strong>Password successfully changed</strong>"; 
      }
      ?>
      </td>
        </tr>
      </table>
      <?php }?>
      Last edited by Atli; Apr 12 '10, 07:22 AM. Reason: Added [code] tags.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        To quote the manual (the link I posted earlier
        Meaning, if you are expecting the query to affect one row, you would check to see if the function returns 1. If it does not then the query did not work as expected.

        Comment

        • Vkas
          New Member
          • Feb 2009
          • 78

          #5
          can you elaborate a little more so that i can reach with a solution

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            You use the mysql_affected_ rows function on the result of a mysql_query call to find out how many rows the query affected.

            In your case, you need to find out if the query affected any rows at all, so you use it to see if one or more rows were affected. If there were one ore more rows affected, you print the success message. Otherwise you print an error message.

            In simple pseudo-code, you could write that as:
            [code=text]rowsAffected = mysql_affected_ rows(result)
            if rowsAffected > 0
            Print success message
            else
            Print error message[/code]

            Comment

            • Vkas
              New Member
              • Feb 2009
              • 78

              #7
              my $result= mysql_query("Qu ery")
              rowsAffected = mysql_affected_ rows($result)
              if rowsAffected > 0
              Print success message
              else
              Print error message

              it gives error

              Warning: mysql_affected_ rows(): supplied argument is not a valid

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                This is a common error,
                whereas mysql_num_rows expects a result resource,
                mysql_affected_ rows() expects a link (database) resource.
                i.e the return from mysql_connect()

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  O yea, that's right. I forgot that it took the database link, rather than the query result. Haven't worked with the old mysql functions in ages :)

                  Comment

                  • Vkas
                    New Member
                    • Feb 2009
                    • 78

                    #10
                    so any other suggestion!!

                    mysql_query returns 1 on both condition whether the row is effected or not

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Another suggestion isn't needed. The one we've been talking about works fine, you just need to use the database link in the mysql_affected_ rows() function rather than the query result.

                      Comment

                      • Vkas
                        New Member
                        • Feb 2009
                        • 78

                        #12
                        ok i had got the solution with another logic thanks for you both for cooperation

                        i had checked firstly the old password from the database by fetching the value

                        then i compared and used if else simply

                        Comment

                        • dlite922
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1586

                          #13
                          Also keep in mind, if the user sets the password to his old password (i.e. you update a row, say...first_nam e to John when it's already set to John) the affected rows will be zero even though in some cases this is what you expect, "a blind update".

                          So I wouldn't build an if statement around an update query that expects it to always return a value greater than zero.

                          Comment

                          • NettSIte
                            New Member
                            • Jun 2009
                            • 7

                            #14
                            Just a thought - basing an update on "where password =" is a tad risky. If your passwords are not unique, you will update all records with the same password, and if passwords are unique, you have opened a possibility for someone to figure out existing passwords.

                            Comment

                            Working...