Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

trapping mysql_errno() & updating db

Posted on 2002-04-10
7
Medium Priority
?
1,160 Views
Last Modified: 2008-03-06
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
Comment
Question by:manju70
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6930715
YOu should try this

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

Harry
0
 
LVL 8

Expert Comment

by:us111
ID: 6930805
if(!($val==mysql_query($addStmt)) && (mysql_errno()==1062)){
0
 

Author Comment

by:manju70
ID: 6930852
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6931005
Try this

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

}
0
 
LVL 5

Expert Comment

by:andriv
ID: 6931174
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
 
LVL 5

Accepted Solution

by:
andriv earned 140 total points
ID: 6931183
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
 

Author Comment

by:manju70
ID: 6936265
I wanted to share the marks between andriv & harwantgrewal.
how to do?
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question