/    Sign up×
Community /Pin to ProfileBookmark

Checking the existence of a database record

i have a itemCode field which is the primary key but is entered by the user.
how do i check if the item code already exist to prevent error entry of the same itemCode?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@HuevoosJun 07.2007 — neither MySql nor Postgress allow a duplicate primary key, so you should just check the affected rows http://www.php.net/manual/en/function.mysql-affected-rows.php after the insert, or you could do a simple "select itemCode from table where itemCode = $itemCode" before the insert or you could make a trigger, but that's more mysql related.
Copy linkTweet thisAlerts:
@engrauthorJun 07.2007 — found the solution!

$select="Select * from tblitem where ItemCode='$code'"; //here $code is the textbox containing the item code.

$res=mysql_query($select,$db);

if (mysql_num_rows($res)=0)

{
Copy linkTweet thisAlerts:
@engrauthorJun 07.2007 — found the solution!

$select="Select * from tblitem where ItemCode='$code'"; //here $code is the textbox containing the item code.

$res=mysql_query($select,$db);

if (mysql_num_rows($res)=0)

{

insert query

}

else

$msg="Item code already exists!";
×

Success!

Help @engr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...