As part of my class file, I have a function that will update info int he database. The problem comes when the user performs an update, but the info is exactly the same, i.e. the user goes to the update page and clicks submit without changing anything. I use mysql_affected_rows to determine update success or not, and since nothing was affected, it will return false. Is there an easy way around this? Right now I query the record and compare it to the submited data and return true if they match; I'm sure there must be an easier way of doing this.

	function UpdateGame($Database)
	{
		$DB = mysql_connect("localhost", "", "")
			or die ("Could not connect to server: " . mysql_error());
		mysql_select_db ($Database, $DB)
			or die ("Could not connect to the database: " . mysql_error());

	$SQL = "select * from Games where GameID = '$this->GameID'";

	$Game = mysql_query($SQL)
		or die("Could not query the database: " . mysql_error());

	$Row = mysql_fetch_array ($Game, MYSQL_ASSOC);

	if ($Row['Name'] == $this->Name && $Row['ReleaseDate'] == $this->ReleaseDate && $Row['Comment'] == $this->Comment && $Row['PhotoID'] == $this->PhotoID)
	{
		return True;
	}
	else
	{
		$SQL = "update Games set Name = '$this->Name', ReleaseDate = '$this->ReleaseDate', Comment = '$this->Comment', PhotoID = '$this->PhotoID' where GameID = '$this->GameID'";

		$Game = mysql_query($SQL)
			or die("Could not query the database: " . mysql_error());

		if (mysql_affected_rows () > 0)
		{
			return True;
		}
		else
		{
			return False;
		}
	}
}
}

    You don't have to use mysql_affected_rows(). Instead, check the value of $Game to see if the query succeeded. It is possible for the query to succeed but not affect any rows. That is, if your UPDATE query is formatted properly but no rows need to be updated... you get the idea!

    http://us2.php.net/mysql_query

      Write a Reply...