sql - PHP form to update string, always returns success -


i trying use php form update sql database.

after spending hours able update database trouble if row not found matching string, still returns message "updated data successfully". checked database , there no other enteries have been entered want make user aware when mistype id.

thanks

if (isset($_post['update'])) {     $dbhost = 'localhost';     $dbuser = 'user';     $dbpass = 'password';     $conn = mysql_connect($dbhost, $dbuser, $dbpass);      if (! $conn ) {         die('could not connect: ' . mysql_error());     }      $uniqueid = mysql_real_escape_string($_post['uniqueid']);     $referral = mysql_real_escape_string($_post['referral']);      mysql_select_db('database');      $sql = "update tablename set referral = '$referral' id = '$uniqueid'";       $retval = mysql_query( $sql, $conn );      if(! $retval ) {         die('could not update data: ' . mysql_error());     } else {         echo "updated data successfully\n";     }     mysql_close($conn); } 

thanks

that's because update doesn't change rows not failure. it's query changed/found/affected no rows.

you need check mysql_affected_rows() instead.

e.g.

$result = mysql_query(...); if ($result === false) {     die(mysql_error()); // blew } else if (mysql_affected_rows() == 0) {     ... nothing changed } 

a failure in sql due syntax errors, communications errors, or "meta" errors violating constraints. empty result set valid result set happens empty.


Comments