This topic is locked

unique vales

7/3/2008 9:46:31 AM
PHPRunner General questions
J
jskewes author

Hi -

I have a ticketing system that I am building.

I have an auto-increment field called 'index' that is the primary key.
I also have a field called 'stock number' that I do not want to allow duplicate values in.
Q; What it s the best way to prevent dupes in this field? I built these tables in PHPR and have used any thing else to edit them.
Thanks,

/john

A
alang 7/6/2008

You can force it in the database (are you using MySQL?) but the error message you get back is not really user friendly. The other way is to put in some event code in to check before saving the record. From memory there have been other posts on this subject.

H
horsey_kim 9/3/2008

Using some of the code I found Jane had given someone else with a slight change it worked! of course you have to use your table name and field names.
ADD SCREEN event in the BEFORE RECORD ADDED:
global $conn;

$strSQLExists = "select * from tablename where fieldname='".$values["fieldname"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

echo "<center><b><font color=red>Field name already exist</font></b></center>";

return false;

}

else

{

return true;

}