Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Update problem

Tags:

php

mysql

$result=mysql_query(" UPDATE xxxxxx_users SET User_Password='$Password' WHERE FstName='$First' AND LstName='$Last'",$db)  or die ("Password update successful!");
echo "Update failed, unknown user";

This correctly updates the db when the first and last names match and the db is not affected when they don't. My only issue is I always display the Update failed, unknown user message. what did I do wrong? Thanks.

like image 255
Bill Avatar asked Dec 10 '25 07:12

Bill


1 Answers

The mysql_query function returns true when an SQL query is successful:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Your code is assuming that the query returns the number of rows effected. Use the mysql_affected_rows function for this purpose:

$result=mysql_query(" UPDATE xxxxxx_users SET User_Password='$Password' WHERE FstName='$First' AND LstName='$Last'",$db)
if (mysql_affected_rows() > 0)
  die ("Password update successful!");
else
  echo "Update failed, unknown user";
like image 107
leepowers Avatar answered Dec 12 '25 22:12

leepowers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!