Link to home
Start Free TrialLog in
Avatar of manju70
manju70

asked on

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
Avatar of harwantgrewal
harwantgrewal
Flag of Australia image

YOu should try this

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

Harry
if(!($val==mysql_query($addStmt)) && (mysql_errno()==1062)){
Avatar of manju70
manju70

ASKER

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


Try this

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

}
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;
   }
}
ASKER CERTIFIED SOLUTION
Avatar of andriv
andriv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manju70

ASKER

I wanted to share the marks between andriv & harwantgrewal.
how to do?