User writes a series of tags (, separated) and posts the form.
I build an array containing the tags and delete dupes with array_unique() php function.
I’m thinking of doing:
- go through the array with foreach($newarray as $item) { … }
- check each $item for existence in the tags mySQL table
- if item does not exists, insert into tags table
Is there a FASTER or MORE OPTIMUM way for doing this?
I’m hoping that people can comment on this method. Intuition tells me its probably not the best way to go about things, but what do you all think?
Instead of making an extra call to the DB to find duplicates, just add a unique constraint in the DB so that tags cannot be inserted twice. Then use
INSERT IGNORE...when you add new tags. This method will ignore the errors caused by duplications while at the same time inserting the new items.