Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch SQL Update Error 1

Status
Not open for further replies.

lanm

Programmer
Jul 7, 2005
244
US
My code is listed below.
Everything works fine, but I'd like to be able to return something just in case the Update SQL didn't work. Is this already taken care of with the die(mysql_error() below?

Thanks!


<?PHP
#when the ZNumber from text box is posted
if (isset($_POST['Znumber']))
{
$IDFound = "This is a valid ZNumber and Password";
$IDNotFound = "Either your ZNumber or Password are
incorrect. Please try again!";
$Password = "Test1234!";
$strZNumber = $_POST['Znumber'];
#first see if both the ZNumber and password are useable
If ($totalRows_Recordset2==1 && $_POST['Password']==$Password)
{
#Do the update of videoseen
#1st make the connection
$conn = mysql_connect ('localhost','usr','pwd');
#now select the db
mysql_select_db ("videocheck");
$sqlUpdate = sprintf("Update table1 set videoseen=1 Where ZNumber
= ".$_POST['Znumber']);
$rsUpdate = mysql_query($sqlUpdate, $conn) or die(mysql_error());
echo "You now have credit for the video";
}
Else
#Let 'em know the two entries won't allow update!
{echo $IDNotFound;}
}
?>
 
use affected_rows method

$rsUpdate = mysql_query($sqlUpdate, $conn) or die(mysql_error());
echo (mysql_affected_rows($rsUpdate)>0 ? "The update is successful" : "The Update fails");
echo "You now have credit for the video";
 
Thanks WoodyRoundUp!

I'll try that!
 
I don't think I'd use mysql_affected_rows(). Since your code makes use of the "or die(...)" construct, it already takes into account that on an UPDATE query, mysql_query() returns a TRUE on success and a FALSE on failure. Just make better use of that fact:

$sqlUpdate = sprintf("Update table1 set videoseen=1 Where ZNumber = ".$_POST['Znumber']);
$rsUpdate = mysql_query($sqlUpdate, $conn);
if ($rsUpdate === FALSE)
{
//print some useful, pretty message
}



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214, I know you have been answering lots of question here.

But, I have been having trouble with your method many many times, because it does not catch the error.
It will only catch QUERY structure error, but not data/logical error.

If the query is valid, and it does not update the data, it should still fail. Therefore, mysql_affected_rows will be the best to use.
 
Sorry. You titled this thread "Catch SQL Update Error", so I thought we were trying to handle errors. An UPDATE query that does not update any rows is not an error, it's just a query that doesn't update any rows.


I suggest you might want to try something like:

if ($rsUpdate === FALSE || mysql_updated_rows() == 0)
{
//handle an error and a 0-row update gracefully
}


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I guess this does not work, as-is:
Code:
$sqlUpdate = sprintf("Update table1 set videoseen=1 Where ZNumber 
= ".$_POST['Znumber']);

You are missing an end-quote there.
I would also look into this:

I wont bother commenting more on the mysql injection attacks as of now, but if you wish, search the forum, or read about it on the URL I posted above.

I've made many tips about this before, but I'm sick today, so my mind is not full of resources.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top