472,378 Members | 1,229 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  3. function checkempty(obj,msg){
  4.     if(obj.value==''){
  5.         alert(msg);
  6.         obj.focus()
  7.         return false;
  8.     }
  9.     return true;
  10. }
  11. function confirmpass(obj,obj1,msg){
  12.     if(obj1.value!=obj.value){
  13.         alert(msg);
  14.         obj1.select()
  15.         obj1.focus()
  16.         return false;
  17.     }
  18.     return true;
  19. }
  20. function chkChangePass(){
  21.     if(checkempty(document.frmChangePass.oPass,"Information: Enter Old Password")==false) return false;
  22.     if(checkempty(document.frmChangePass.NPass,"Information: Enter New Password")==false) return false;
  23.     if(checkempty(document.frmChangePass.CNPass,"Information: Confirm New Password")==false) return false;
  24.     if(confirmpass(document.frmChangePass.NPass,document.frmChangePass.CNPass,"Information: New and Confirm Password should be same")==false) return false;
  25.     return true;
  26. }
  27. </script>
The form code is as follow
Expand|Select|Wrap|Line Numbers
  1. <form name="frmChangePass" method="post" action="Changepass.php?action=Confirm&amp;id=1" onSubmit="return chkChangePass();">
  3.           <table width="100%" border="0" cellspacing="2" cellpadding="1">
  4.               <tr bordercolor="#CCCCCC">
  5.                 <td width="30%">Old Password:</td>
  6.                 <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>
  7.               </tr>
  8.               <tr bordercolor="#CCCCCC">
  9.                 <td>New Password: </td>
  10.                 <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>
  11.               </tr>
  12.               <tr bordercolor="#CCCCCC">
  13.                 <td>Confirm New Password: </td>
  14.                 <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>
  15.               </tr>
  17.               <tr bordercolor="#FFFFFF" bgcolor="<?php echo $rClr?>">
  18.                 <td align="left" valign="middle" bordercolor="#CCCCCC" bgcolor="<?php echo $rClr?>" class="welcome">&nbsp;</td>
  19.                 <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>
  20.               </tr>
  21.             </table>
  22.             </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

Expand|Select|Wrap|Line Numbers
  2. <?php }if($action=="Confirm"){
  3. $id=$_GET['id'];
  4. $oldpassword=$_POST['oPass'];
  5. $CNpassword=$_POST['CNPass'];
  7. $update= "Update `users` Set `user_pass`='$CNpassword' Where `campus_id`=1 AND `user_pass`='$oldpassword'";
  8. $result=mysql_query($update) or die(mysql_error()); 
  10. php echo "<strong>Password successfully changed</strong>"; 
  11. }

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
Apr 12 '10 #1
13 9035
5,058 Expert 4TB
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.
Apr 12 '10 #2
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

Expand|Select|Wrap|Line Numbers
  1. <?php }if($action=="Confirm"){
  2. $id=$_GET['id'];
  3. $oldpassword=$_POST['oPass'];
  4. $CNpassword=$_POST['CNPass'];
  6. $update= "Update `users` Set `user_pass`='$CNpassword' Where `campus_id`=1 AND `user_pass`='$oldpassword'";
  7. $result=mysql_query($update) or die(mysql_error()); 
  8. if(mysql_affected_rows ($result) == false)
  9. {
  10.  echo "<strong> Old password wrong please retype it again<</strong>"; 
  11. }
  12. else
  13. {
  14.  echo "<strong>Password successfully changed</strong>"; 
  15. }
  16. ?>
  17. </td>
  18.   </tr>
  19. </table>
  20. <?php }?>
Apr 12 '10 #3
5,058 Expert 4TB
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.
Apr 12 '10 #4
can you elaborate a little more so that i can reach with a solution
Apr 12 '10 #5
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. rowsAffected = mysql_affected_rows(result)
  2. if rowsAffected > 0
  3.     Print success message
  4. else
  5.     Print error message
Apr 12 '10 #6
my $result= mysql_query("Query")
rowsAffected = mysql_affected_rows($result)
if rowsAffected > 0
Print success message
Print error message

it gives error

Warning: mysql_affected_rows(): supplied argument is not a valid
Apr 12 '10 #7
code green
1,726 Expert 1GB
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()
Apr 12 '10 #8
5,058 Expert 4TB
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 :)
Apr 12 '10 #9
so any other suggestion!!

mysql_query returns 1 on both condition whether the row is effected or not
Apr 12 '10 #10
5,058 Expert 4TB
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.
Apr 12 '10 #11
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
Apr 12 '10 #12
1,584 Expert 1GB
Also keep in mind, if the user sets the password to his old password (i.e. you update a row, say...first_name 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.
Apr 12 '10 #13
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.
Apr 13 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

by: vishal | last post by:
hi i am building aan application which will send mail to user when he registers on my site and i am checking whther the email id is working properly or not by sending his email and then cheking...
by: Siu | last post by:
Hi, in my ASP.NET (C#) application I would like to check in advance if a node exist: my code is as follow: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(XML_FILENAME); XmlNode...
by: kiran | last post by:
How to check whether the given application is running or not in the current machine. From, my cshartp program I want check whether the yahoo messenger process is running ro not..? if the process is...
by: Jiho Han | last post by:
What is the best way to check whether the page is simply a postback or the form has been submit with the intention of doing something? In the olden days, I used to check for a form field name...
by: zimmy | last post by:
Hi, Is it possible to check whether ASP.NET is installed on a machine, within a program that I'm writing in C#? And if it's installed, can I check if it is enabled? How? Thanks
by: ItsMe | last post by:
Hi, I've 50 MDI Forms in my project, so trying to create MDI Child Form from this procedure. But the problem is, unable to declare as "NewFormName". It gives me an error. Is there any other...
by: SoFaraway | last post by:
Dear all, In C, how do we check whether a string starts with a substring? E.g., char *str = ...; char *substr = ...; How do we check whether str starts with substr? Thank you very much!
by: foolsmart2005 | last post by:
There are 10 webpages on the host, e.g. 001.html, 002.html, 003,html, 004.html, ......010.html I want to check whether the page is the last page. How can I do. In the index.html -Go to last...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.