Miri Miri - 1 year ago 121
MySQL Question

php tag system - if tag already exists in table

i am establisihing a tags system and don't want a tag in the tags table to be added if it already exists. For this, do i need to select and verify a table with additional query, or can i do the both with one query ?

if (isset($tag_array)) {
foreach ($tag_array as $tag_name) {
$data = array('tag_id' => '', 'name' => $tag_name, 'slug' => '');

$str = $this->db->insert_string('tags', $data);

Answer Source

The database can handle this. Create a unique constraint on the tag name:

alter table tags add constraint unq_tags_tagname unique tags(tagname)

Your insert code will then generate an error if you attempt to insert the same name twice.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download