Phillip
Phillip

Reputation: 1570

Problem with UPDATE MySQL

I have a bit of an issue with my code.

I'm making an administrative panel for users to add things to the database. On occasion, they might try to save data without changing it (open a dialog, click save without changing anything). This, of course, will make mysql_affected_rows() return '0' when checking to see if the UPDATE query worked.

Is there another query to make that will always UPDATE regardless of whether the data is the same or not (or can I modify a simple UPDATE query to always update)?

EDIT

This is for users who don't have programming experience. Of course you wouldn't want to update if there's no reason to, but when a user tries to update and it doesn't happen I end up showing a failure message. Rather than there being something wrong, its just it doesn't need to be updated. I need a way to show the user that, instead of a generic 'failure' message. If it failed for another reason, I still need to know.

Upvotes: 0

Views: 337

Answers (1)

Evan Mulawski
Evan Mulawski

Reputation: 55334

From the MySQL Documentation:

If you set a column to the value it currently has, MySQL notices this and does not update it.

Instead of checking mysql_affected_rows, just check to see if the query was successful:

if(!mysql_query("UPDATE ..."))
{ 
  //failure
}
else
{
    $verification = mysql_query("SELECT ROW_COUNT() as rows_affected");
    $row = mysql_fetch_row($verification);
    $rows_affected = $row[0];

    if ($rows_affected > 0)
    {
        //update was performed
    }
    else
    {
        //no update was needed
    }
}

Upvotes: 4

Related Questions