SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with UPDATE query

    Hi all, I'm messing around trying to learn more about db interactions and I have an "edit" page which pulls user info and populates it into text fields for editing/updating the db.

    If I make any change to the values and submit, I don't get any errors an my updates show up in the db, so all is good there.

    If I just submit without making any changes, my else branch is hit, so mysql_affected_rows is not reporting a row affected, even though aren't I replacing the values in the db with the exact same values? Shouldn't that still report that row being updated?

    Code:
    //if no errors UPDATE DB
    if (empty($errors)) {
        $query = "UPDATE users
                  SET first_name='$firstname', last_name='$lastname', email='$email', state='$state'";
    		
        //if changing password				
        if($newpass1){
            $query .= ", pass=md5('$newpass1')";
        }
    						
        $query .= " WHERE user_id = {$_SESSION['user_id']}";
    				
        $result = @mysql_query($query, $dbc) OR trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error($dbc));
    				
        if (mysql_affected_rows($dbc) == 1) { //if it ran ok
            echo $query . '<br> ok';
        } else { //did not run ok
            echo $query .'<br> noob';
        }
    					
    }

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I updated to this:

    Code:
     if (mysql_affected_rows($dbc) > -1)
    And it seems to work, sound right?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •