Jump to content


Photo

Checking if data in database was updated from a query


  • Please log in to reply
1 reply to this topic

#1 alexville

alexville
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 01 April 2006 - 01:41 PM

I've been trying for days to get this too work.. I need help with this code:
mysql_select_db("alexg_web",$dbcnx);
$updatesql = "UPDATE whoamipeople SET guessed = '$newguessed' WHERE id = '$guessid'";
$resultupdateguess = mysql_query($updatesql, $dbcnx);
if (!$resultupdateguess) {
   echo "Sorry! That guess was incorrect. Nice Try!\n";
   echo 'MySQL Error: ' . mysql_error();
   exit;
}

If no data is updated because the id doesn't match the guessed id in the database... I want it to display this message.
if (!$resultupdateguess) {
   echo "Sorry! That guess was incorrect. Nice Try!\n";
   echo 'MySQL Error: ' . mysql_error();
   exit;
}

But that code doesn't work! what am i doing wrong?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 April 2006 - 06:58 PM

[!--quoteo(post=360602:date=Apr 1 2006, 02:41 PM:name=alexville)--][div class=\'quotetop\']QUOTE(alexville @ Apr 1 2006, 02:41 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
But that code doesn't work! what am i doing wrong?
[/quote]

You are assuming that the query failed just because there is no record with id = $guessid.

The query will only fail and return false if there is an error. Not finding a record does not constitute an error.

Try
if (mysql_affected_rows($resultupdateguess) == 0) {
     echo "Sorry! That guess was incorrect. Nice Try!\n";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users