Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

trapping mysql_errno() & updating db

Posted on 2002-04-10
7
Medium Priority
?
1,159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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