This topic is locked

Duplicate Entry Prevention

11/8/2005 9:36:20 PM
PHPRunner General questions
K
ke5rs author

Hello

I have a table with about 20 fields and two of those fields are unique.

Of those two unique fields, one is auto incremented and the other is manually entered.

How can I prevent duplicate entries of the manually entered field?

Example,

I only want one person in the database with the name "John"

I would like the user to be prompt that there already exists the name "John" (not case sensitive)

I already have this name field as a KEY field.

Thank you.

John

admin 11/9/2005

John,
the most reliable way to avoid duplicates is to create unique index on this field in your table.

The syntax can be as the following (for MySQL database):

CREATE UNIQUE INDEX i_IndexName ON Table1 (Field1);


Also you can add your custom code to Add and Edit pages to look for dulicate values before the record is inserted or updated.

Then you'll be able to show more user-friendly messages.

You can insert your code before this fragment in ..._add.php file:

//  make SQL string

    $strSQL = "insert into ".AddTableWrappers($strTableName)." ";



You can use $avaluesarray to access values enterd by user. Here is the syntax:

if($avalues["`SerialNumber`"]=="'1234-abcd-cvg4-aaaa'")



Here is the place to insert your code in ..._edit.php file:

foreach($evalues as $ekey=>$value)

    $strSQL.=$ekey."=".$value.", ";

And array with values is $evalues.