mysql duplicate primary key when insert

wid is primary key How can we make user known  

'Your Id is duplicate"

incase we can not insert because of duplicate key
$AccNo = $_POST['AccNo'];
$MemberName = $_POST['MemberName'];
$Amount = $_POST['Amount']; 
echo $AccNo[0] ;
echo "<br>";
echo "$MemberName[0]";
$insertSQL = "INSERT INTO balanceborow (wid, wname, amout, DateBalance) VALUES ('$AccNo[0]', '$MemberName[0]', '$Amount', now())" ;
 mysql_select_db($database_village, $village);
 $Result1 = mysql_query($insertSQL, $village) or die(mysql_error());

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This code should work.
$Result1 = mysql_query($insertSQL, $village);
if (mysql_errno($village) == 1062) // ER_DUP_ENTRY
 // inform user that this wid already exists
elseif (mysql_errno($village) != 0) // Other error
 // process normally

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
u could check for wid before doing your insert, if it is there, generate an error code
$r = mysql_query ("select * from table where wid=$var')
if (mysql_num_rows($r)>0) die ('duplicate');

or you can wait for mysql error (provided that wid is the primary key to your table)
after executing your query, check for the generated error
if (mysql_error() ==1061) die ('duplicate key');
Loganathan NatarajanLAMP DeveloperCommented:
i would suggest to check those primary key whenever you make insert entry to the database... that is the best way to do it.,

so,  before insert, just get the "wid" and check it with database whether it is exist or not... if not process inserting.. else inform to the user saying that already exist...

I'd suggest to try to insert, then check the mysql_errno().
You'll avoid a concurrent query to be sent by another php thread between your SELECT and your INSERT, resulting in a bug in your application.
You can do it with a SELECT before, but to be sure that there is no collision with another thread, you'd have to use a LOCK TABLES query before, not necessary IMO here.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.