Skip to main content
Known Participant
February 24, 2012
Question

error checking if record updated

  • February 24, 2012
  • 1 reply
  • 1425 views

I am having trouble with this code.  If the user submits the form, it always returns that the account has been updated. 

if(isset($_POST['changeUname'])) {

    $newUname = $_POST['new_uname'];

    if($newUname != "" && $newUname != null) {

        $query = "SELECT uname, change_uname FROM members WHERE ID='$userID'";

        $result = mysql_query($query) or die(mysql_error());

       

        if (mysql_num_rows($result) > 0) {

            $row = mysql_fetch_array($result) or die(mysql_error());

            if($row['change_uname'] == 0) {

                mysql_query("UPDATE members SET uname = '$newUname', change_uname = '1' WHERE ID = '$ID'");

            } else {

                $changeUnameErrors[] =  "You cannot change your username.  You have already changed it.";

            }

        }

    } else {

        $changeUnameErrors[] =  "Please enter a valid username!";

    }

    if (mysql_affected_rows() > 0) {

        echo "<p><span style='color:red; font-weight:strong; font-size:12px;'>Your account has been successfully updated!</span><p>";

    }

}

This topic has been closed for replies.

1 reply

Participating Frequently
February 24, 2012

Troubleshoot by echoing the value of $row['change_uname'] and mysql_affected_rows() in the appropriate spot. One of these is most likely not what you are expecting.

Known Participant
February 25, 2012

mysql_affected_rows() = 1.... how can this be? nothing was changed....

well anyway, i  guess this is one way to fix it....

$query = "SELECT uname FROM members WHERE ID='$userID' AND change_uname='0'"; (0 being false)

tacked on this else statement if that query fails:

else {

        $changeUnameErrors[] =  "Please enter a valid username!";

    }

Participating Frequently
February 25, 2012

>mysql_affected_rows() = 1.... how can this be? nothing was changed....

We can't answer that without knowing what the existing data in the database is, and what the data submitted from the form is.

>well anyway, i  guess this is one way to fix it....

Sure, but it's always good to know why it's not working the way you think it should.