Click to See Complete Forum and Search --> : Checking the existence of a database record


engr
06-06-2007, 09:16 PM
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?

Huevoos
06-06-2007, 10:32 PM
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.

engr
06-07-2007, 10:15 AM
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)
{

engr
06-07-2007, 10:17 AM
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!";