MySQL upgrade problem

$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 db when the first and last name match, and db does not affect when they do not. My only problem is that I always show an โ€œUnsuccessful updateโ€ error message, unknown. What have I done wrong? Thank.

+3
source share
3 answers

The mysql_query function returns true if the SQL query completes successfully:

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

, . mysql_affected_rows:

$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";
+5

, ...

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

echo "Password update successful!";
+2

If the first part (mysql_query) is true, there is no need to evaluate the second or part (die). You can use and instead.

And please read about SQL Injections.

Suggestion for your coding style:

Something like that:

if ($result)
   ...success
else 
   ...fail

is more readable.

0
source

Source: https://habr.com/ru/post/1733446/


All Articles