This topic is locked

Making a field unique

1/24/2008 7:23:20 PM
PHPRunner General questions
H
horsey_kim author

I am sure I saw it somewhere. But how do I make a field like username require when entering in a new one that it be unique?
Needs to error by saying already exist try another
Code? settings in php runner? database?
Something like that <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=7355&image=1&table=forumtopics' class='bbc_emoticon' alt=':)' />
Kim

A
alang 1/24/2008

You can certainly do it in the database. Using DB admin tool like SQLyog, just add an index to the table for the particular field and then mark it as unique. If you try and add a record with the same data in that field in your PHPR project, the system will error but the message may not be as "user-friendly" as what you might be after.

H
horsey_kim author 1/28/2008

I thought I saw something in phpr. I did want it to be user friendly and not some mysql error that confuse people.
Any ideas other than modify the database field at the database.
Kim <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=25258&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
Thanks for the response AlanG <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=25258&image=2&table=forumreplies' class='bbc_emoticon' alt=':)' /> - I will probably do that as a last result.

H
horsey_kim author 2/4/2008

UPDATE: 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;

}