• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1178
  • Last Modified:

trapping mysql_errno() & updating db

hai all,

my intention is to trap mysql_errno while I try to insert data in to mysql. the coding is as below

$addStmt = //sql query for insert statements

if(!($val= mysql_query($addStmt)) && (mysql_errno()=1062)){
 $addStmt1="update $tableName3 set $tableName3.dop_dbb
           =$value4";
 mysql_query($addStmt1) or die(mysql_error());
}//if statement is to trap for duplicate key in db

elseif(!($val= mysql_query($addStmt))){mysql_query($addStmt1);
         echo mysql_error();
}// this if statement is to trap other errors
else{
mysql_query($addStmt) or die (mysql_error());
echo "success";
}// this is for inserting in to db if other if clauses r turns false.

here I my problem is I get an error msg in my first if statement.

is anything wrong with my coding.

please let me know.
thanks
0
manju70
Asked:
manju70
  • 2
  • 2
  • 2
  • +1
1 Solution
 
harwantgrewalCommented:
YOu should try this

if(!($val= mysql_query($addStmt)) && (mysql_errno()==1062))

Harry
0
 
us111Commented:
if(!($val==mysql_query($addStmt)) && (mysql_errno()==1062)){
0
 
manju70Author Commented:
hai all,

thanks for locating my error.(mistake)

I have addtional query pertaining to above code.
as I mentioned earlier.
1) first if to trap mysql_errno()=1062
2) 2nd if is to check for any other errors other than above condition
3) 3rd is if nothing happens for above 2 condition just insert the values in to db instead of updating.

can anyone tell me the way I check my 2nd & 3rd condition is correct or is there any better way of putting my conditions.

thanks


0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
harwantgrewalCommented:
Try this

$addStmt = //sql query for insert statements
$val=mysql_query($addStmt);
if(!($val) && mysql_errno()==1062){

}
0
 
andrivCommented:
To trap 1062 errors AND if other error display error and exit do but if NO errors enter information do:

$addStmt = //sql query for insert statements

if(!($val=mysql_query($addStmt)))
{
   //Check if error is 1062
   if(mysql_errno() == 1062)
   {
     trap error
   }
   else //If error but not 1062 display error and terminate
   {
     echo mysql_error();
     exit;
   }
}
0
 
andrivCommented:
One more thing, you do not need to store an insert in a variable so the original if can be:

if(!(mysql_query($addStmt)))
{
0
 
manju70Author Commented:
I wanted to share the marks between andriv & harwantgrewal.
how to do?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now