Solved

trapping mysql_errno() & updating db

Posted on 2002-04-10
7
1,149 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 35 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now