LanceT Posted January 30, 2007 Share Posted January 30, 2007 Lets say I have an script that inserts some stuff to a database and I want to check if there is a duplicate entry, and if so, prevent the user from adding the stuff (in this case, a username). Any way to do this and using the least amount of system resources? The list that the code will be checking will be very long. Link to comment https://forums.phpfreaks.com/topic/36283-not-allowing-duplicate-entries-in-a-field/ Share on other sites More sharing options...
artacus Posted January 30, 2007 Share Posted January 30, 2007 Well on the mysql side, you'll add a unique index on that field. That will prevent dups from being added. But most times you'll check if the record is there; if it is, update it; if not insert it. Link to comment https://forums.phpfreaks.com/topic/36283-not-allowing-duplicate-entries-in-a-field/#findComment-172511 Share on other sites More sharing options...
LanceT Posted January 30, 2007 Author Share Posted January 30, 2007 Well lets say we want to register and it's a username. Obviously we can't have duplicate usernames. So I want to display an error message if the username is taken.I'm pretty sure this syntax won't work, but something like this.[code]if ( $username exists ) {echo "sorry, that username is taken";}[/code]Some code would be helpful!Thanks guys. Link to comment https://forums.phpfreaks.com/topic/36283-not-allowing-duplicate-entries-in-a-field/#findComment-172713 Share on other sites More sharing options...
fenway Posted January 30, 2007 Share Posted January 30, 2007 Well, like artacus said, if you have a unique index, you can simply attempt to add it, and catch the error. Link to comment https://forums.phpfreaks.com/topic/36283-not-allowing-duplicate-entries-in-a-field/#findComment-172954 Share on other sites More sharing options...
LanceT Posted February 3, 2007 Author Share Posted February 3, 2007 [quote author=fenway link=topic=124651.msg517276#msg517276 date=1170181761]Well, like artacus said, if you have a unique index, you can simply attempt to add it, and catch the error.[/quote]How do i add the unique index? Link to comment https://forums.phpfreaks.com/topic/36283-not-allowing-duplicate-entries-in-a-field/#findComment-175855 Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 Depends through what interface.. but ADD UNIQUE INDEX indexName ( columnName) will work too. Link to comment https://forums.phpfreaks.com/topic/36283-not-allowing-duplicate-entries-in-a-field/#findComment-177201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.